seekbar not working with video view when handle and move it - java

I am building a simple video player Ui and I have a : seekbar and two buttons to controle the video view, when I click on any point of seekbar every thing is ok and video view progress will be the same with seekbar and the buttons is working too ! but,when I handle the seekbar thumb and move it ,then leave it, every thing messing up and the seekbar and the buttons will never work again, so I am not getting an Exception error it's just will not work again. why that happening?
Activity
public class Player extends AppCompatActivity {
View controller;
Handler handler;
SeekBar seekBar;
VideoView videoView;
ImageButton play_btn, pause_btn;
ImageButton forward_btn, replay_btn;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
handler = new Handler();
videoView = (VideoView) findViewById(R.id.videoView);
controller = findViewById(R.id.player_controller); // the controller View
hideController(controller);
forward_btn = (ImageButton) findViewById(R.id.playerController_forward_10_Button);
replay_btn = (ImageButton) findViewById(R.id.playerController_replay_10_Button);
forward_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
forward_10_sec();
}
});
replay_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
reply_10_sec();
}
});
play_btn = (ImageButton) findViewById(R.id.playerController_playButton);
pause_btn = (ImageButton) findViewById(R.id.playerController_pauseButton);
play_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
play();
}
});
pause_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pause();
}
});
seekBar = findViewById(R.id.playerController_seekBar); // video seekBar
seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
videoView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (controller.getVisibility() == View.INVISIBLE)
showController(controller);
else
hideController(controller);
}
});
Intent intent = getIntent();
if (intent != null)
setVideoSource(intent);
}
private void play() {
if(!videoView.isPlaying()) {
videoView.start();
play_btn.setVisibility(View.INVISIBLE);
pause_btn.setVisibility(View.VISIBLE);
}
}
private void pause() {
if(videoView.isPlaying()) {
videoView.pause();
pause_btn.setVisibility(View.INVISIBLE);
play_btn.setVisibility(View.VISIBLE);
}
}
private void forward_10_sec() {
if (videoView.canSeekForward())
videoView.seekTo(videoView.getCurrentPosition() + 1000);
}
private void reply_10_sec() {
if (videoView.canSeekBackward())
videoView.seekTo(videoView.getCurrentPosition() - 1000);
}
// to show the controllers
private void showController(View controller) {
controller.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
hideController(controller);
}
}, 6000);
}
// to hide the controllers
private void hideController(View controller) {
controller.setVisibility(View.INVISIBLE);
}
// to set video source from the main app or file manager
private void setVideoSource(Intent intent) {
... some codes to set path ...
play();
showController(controller);
updateSeekBarProgress();
}
// to move seekBar to real video progress
private void updateSeekBarProgress() {
Player.this.runOnUiThread(updateSeekBar);
}
private Runnable updateSeekBar = new Runnable() {
#Override
public void run() {
seekBar.setMax(videoView.getDuration() / 1000);
seekBar.setProgress(videoView.getCurrentPosition() / 1000);
seekBar.postDelayed(this,500);
}
};
SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
videoView.seekTo(i*1000);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
}
activity_player
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context=".Player">
<VideoView
android:id="#+id/videoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include
android:id="#+id/player_controller"
layout="#layout/player_controller" />
</androidx.constraintlayout.widget.ConstraintLayout>
player_controller
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent_black">
<ImageButton
android:id="#+id/playerController_playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/play_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playerController_pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/pause_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playerController_replay_10_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/replay_10_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/playerController_playButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/playerController_forward_10_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/forward_10_icon_48"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/playerController_playButton"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="#+id/playerController_seekBar"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:progressBackgroundTint="#color/white"
android:progressTint="#color/white"
android:secondaryProgressTint="#color/light_purple"
android:thumbTint="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/playerController_playButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
so, in a simple way : I want to set video view progress like seekbar progress with any way that I move the seek bar with.
(now it's just works when click on it and when handle and move it , nothing works again).
thanks...

It's finally has been done with that :
SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
videoView.seekTo(i * 1000);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
handler.removeCallbacks(updateSeekBar);
pause();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
videoView.seekTo(seekBar.getProgress()*1000);
play();
updateSeekBarProgress();
}
};

Related

Fragment page not displaying for MediaPlayer Android Studio

I want to make a music player in my fragment page for my android app using android studio. There is no error to the code however when i run the code the fragment page is not appearing the audio player. i follow the code from youtube: https://www.youtube.com/watch?v=rJ3XbXtjNaE&t=385s
this is the code
fragment_med1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#drawable/scene"
tools:context=".Med1Fragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="#drawable/ic_baseline_music_note_24"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="12dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/player_position"
android:text="00:00"
android:textStyle="bold"
android:textColor="#android:color/white"/>
<SeekBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/seek_bar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/player_duration"
android:text="00:00"
android:textStyle="bold"
android:textColor="#android:color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bt_rew"
android:src="#drawable/ic_baseline_fast_rewind_24"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bt_play"
android:src="#drawable/ic_baseline_play_circle_filled_24"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bt_pause"
android:src="#drawable/ic_baseline_pause_circle_filled_24"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bt_ff"
android:src="#drawable/ic_baseline_fast_forward_24"/>
</LinearLayout>
</LinearLayout>
Med1Fragment.java
public class Med1Fragment extends Fragment {
TextView playerposition, playerduration;
SeekBar seekBar;
ImageView btnrew, btnplay, btnpause, btnff;
MediaPlayer mediaPlayer;
Handler handler = new Handler();
Runnable runnable;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
playerposition = playerposition.findViewById(R.id.player_position);
playerduration = playerduration.findViewById(R.id.player_duration);
seekBar = seekBar.findViewById(R.id.seek_bar);
btnrew = btnrew.findViewById(R.id.bt_rew);
btnplay = btnplay.findViewById(R.id.bt_play);
btnpause = btnpause.findViewById(R.id.bt_pause);
btnff = btnff.findViewById(R.id.bt_ff);
mediaPlayer = MediaPlayer.create(getActivity(), R.raw.meditation2);
runnable = new Runnable() {
#Override
public void run() {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
handler.postDelayed(this, 500);
}
};
int duration = mediaPlayer.getDuration();
String sDuration = convertFormat(duration);
playerduration.setText(sDuration);
btnplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnplay.setVisibility(View.GONE);
btnpause.setVisibility(View.VISIBLE);
mediaPlayer.start();
seekBar.setMax(mediaPlayer.getDuration());
handler.postDelayed(runnable,0);
}
});
btnpause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btnpause.setVisibility(View.GONE);
btnplay.setVisibility(View.VISIBLE);
mediaPlayer.pause();
handler.removeCallbacks(runnable);
}
});
btnff.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int currentposition = mediaPlayer.getCurrentPosition();
int duration = mediaPlayer.getDuration();
if(mediaPlayer.isPlaying() && duration != currentposition){
currentposition = currentposition + 5000;
playerposition.setText(convertFormat(currentposition));
mediaPlayer.seekTo(currentposition);
}
}
});
btnrew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int currentposition = mediaPlayer.getCurrentPosition();
if(mediaPlayer.isPlaying() && currentposition > 5000){
currentposition = currentposition - 5000;
playerposition.setText(convertFormat(currentposition));
mediaPlayer.seekTo(currentposition);
}
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser){
mediaPlayer.seekTo(progress);
}
playerposition.setText(convertFormat(mediaPlayer.getCurrentPosition()));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
btnpause.setVisibility(View.GONE);
btnplay.setVisibility(View.VISIBLE);
mediaPlayer.seekTo(0);
}
});
}
#SuppressLint("DefaultLocale")
private String convertFormat(int duration){
return String.format("%02d:%02d"
, TimeUnit.MILLISECONDS.toMinutes(duration)
, TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_med1, container, false);
}
}
I am always confused to write the code for fragment because the usual code i get/learn from (eg:youtube) are written for activity instead of fragment.
really appreciate some help. thank you.

Continuous Scanning QR using Zxing library, failed to open camera

I am creating a qr code scanning app, which continuously scan the qr code and show the result on the same screen without closing the camera.
I am using ZXing library but when I trigger the continuous scanning activity it fails to open the camera. I can't figure out the issue. Please check, I already added camera permission in my manifest.
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="#+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="#+id/buttonsLayout"
android:layout_alignParentTop="true">
</com.journeyapps.barcodescanner.DecoratedBarcodeView>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/buttonsLayout"
android:layout_toLeftOf="#+id/centerHorizont">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pause"
android:onClick="pause" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Resume"
android:onClick="resume" />
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:id="#+id/centerHorizont" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/centerHorizont"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/buttonsLayout"
android:id="#+id/barcodePreview" />
</RelativeLayout>
Java Code:
public class ContinuousCaptureActivity extends Activity {
private static final String TAG = ContinuousCaptureActivity.class.getSimpleName();
private DecoratedBarcodeView barcodeView;
private BeepManager beepManager;
private String lastText;
private BarcodeCallback callback = new BarcodeCallback() {
#Override
public void barcodeResult(BarcodeResult result) {
if(result.getText() == null || result.getText().equals(lastText)) {
// Prevent duplicate scans
return;
}
lastText = result.getText();
barcodeView.setStatusText(result.getText());
beepManager.playBeepSoundAndVibrate();
//Added preview of scanned barcode
ImageView imageView = (ImageView) findViewById(R.id.barcodePreview);
imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
}
#Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.continuous_scan);
barcodeView = (DecoratedBarcodeView) findViewById(R.id.barcode_scanner);
Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39);
barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
barcodeView.decodeContinuous(callback);
beepManager = new BeepManager(this);
}
#Override
protected void onResume() {
super.onResume();
barcodeView.resume();
}
#Override
protected void onPause() {
super.onPause();
barcodeView.pause();
}
public void pause(View view) {
barcodeView.pause();
}
public void resume(View view) {
barcodeView.resume();
}
public void triggerScan(View view) {
barcodeView.decodeSingle(callback);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
Here is the error message, I am getting!
The problem occur because of the permission. I have added code to request permission manually, Now problem is solved.
Thanks to #Roberto Manfreda

How to use Seekbar value as int variable?

I want to make the "a" variable to take the value of the seekbar and use it for the "sleep" param. Now, 'a' is 100. Can this be done with int? What I want: a = the value of the SeekBar as int value instead of 100. I have tried different methods but didn't have any luck. I have also attached the xml code.
public class Blink1 extends AppCompatActivity {
Handler handler ;
ConstraintLayout blink1 ;
int i = 0;
int colors[] = {Color.BLACK, Color.WHITE};
Runnable runnable = new Runnable() {
#Override
public void run () {
blink1.setBackgroundColor(colors[i]);
if(i==1){
i=0;
}
else i++;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getSupportActionBar().hide();
setContentView(R.layout.activity_blink1);
handler = new Handler();
blink1 = (ConstraintLayout) findViewById(R.id.blink2);
final int a;
a=100;
Thread myTread = new Thread() {
public void run() {
try {
while (true) {
sleep(a);
handler.post(runnable);
}
} catch (InterruptedException e) {
}
}
};
myTread.start();
}
}
the xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/blink2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.studios.minimal.blink.Blink1">
<SeekBar
android:id="#+id/seekBar7"
android:max="1000"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.903" />
</android.support.constraint.ConstraintLayout>
Try this
SeekBar seekBar=findViewById(R.id.seekBar7);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
a=seekBar.getProgress();
}
});

On screen rotate only video should be displayed

I have used exoPlayer Library what i am trying to do is i pass data from recyclerview to next activity that works fine video is been played and title as well as desc is been fetched but when i rotate the phone i only want simpleexovideoview to displayed and video is playing but the activity name is still there.
I have used < android:configChanges="orientation|screenSize" > that handles the orientation change following is snapshot of activity
Portrait view
Landscape view
and code is as follows
videoplayer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kaushal.myapplication.MainActivity"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<android.support.constraint.Guideline
android:id="#+id/horizontalHalf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="256dp" />
<TextView
android:id="#+id/VideoTitle"
android:textSize="22sp"
android:text="video title"
android:textStyle="bold"
android:layout_margin="12dp"
android:textColor="#016699"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="#+id/horizontalHalf" />
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="#+id/videoplayer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
app:layout_constraintBottom_toTopOf="#+id/horizontalHalf"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/VideoDesc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:text="Video Desc"
app:layout_constraintLeft_toLeftOf="parent"
android:textSize="18sp"
android:layout_margin="12dp"
app:layout_constraintTop_toBottomOf="#+id/VideoTitle"
tools:layout_editor_absoluteY="477dp"
android:layout_marginLeft="12dp" />
</android.support.constraint.ConstraintLayout>
videoActivity
package com.example.kaushal.myapplication;
/**
* Created by kaushal on 06-09-2017.
*/
public class videoplay extends AppCompatActivity implements
ExoPlayer.EventListener {
TextView vidtitle, videodesc;
String videpath;
SimpleExoPlayer exoplayer;
SimpleExoPlayerView exoPlayerView;
PlaybackStateCompat.Builder videosessionBuilder;
final static String TAG = videoplay.class.getName();
private RelativeLayout.LayoutParams paramsNotFullscreen;
RelativeLayout rl;
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videoplayer);
vidtitle = (TextView) findViewById(R.id.VideoTitle);
videodesc = (TextView) findViewById(R.id.VideoDesc);
exoPlayerView = (SimpleExoPlayerView)
findViewById(R.id.videoplayer);
vidtitle.setText(getIntent().getStringExtra("videotitle"));
videodesc.setText(getIntent().getStringExtra("videodesc"));
videpath = getIntent().getStringExtra("videourl");
mediaSession();
Uri uri = Uri.parse(videpath);
intializePlayer(uri);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
paramsnotfullscreen= (RelativeLayout.LayoutParams)exoPlayerView.getLayoutParams();
RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(paramsnotfullscreen);
params.setMargins(0, 0, 0, 0);
params.height= ViewGroup.LayoutParams.MATCH_PARENT;
params.width=ViewGroup.LayoutParams.MATCH_PARENT;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
exoPlayerView.setLayoutParams(params);
}else if (newConfig.orientation==Configuration.ORIENTATION_PORTRAIT){
exoPlayerView.setLayoutParams(paramsnotfullscreen);
}
} //refrence = https://stackoverflow.com/questions/13011891/make-a-fullscreen-in-only-layout-land-android-when-play-videoview
public void intializePlayer(Uri uri) {
DefaultTrackSelector dfs = new DefaultTrackSelector();
DefaultLoadControl dfc = new DefaultLoadControl();
exoplayer = ExoPlayerFactory.newSimpleInstance(this, dfs, dfc);
exoPlayerView.setPlayer(exoplayer);
//Prepare Media source
String useragent = Util.getUserAgent(this, "MyApplication");
MediaSource mediaSource = new ExtractorMediaSource(uri, new DefaultDataSourceFactory(this, useragent),
new DefaultExtractorsFactory(), null, null);
exoplayer.prepare(mediaSource);
exoplayer.setPlayWhenReady(true);
}
public void releasePlayer() {
exoplayer.stop();
exoplayer.release();
exoplayer = null;
}
public void mediaSession() {
MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(this, TAG);
mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSessionCompat.setMediaButtonReceiver(null);
videosessionBuilder = new PlaybackStateCompat.Builder().setActions(PlaybackStateCompat.ACTION_PLAY |
PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE);
mediaSessionCompat.setPlaybackState(videosessionBuilder.build());
mediaSessionCompat.setCallback(new mediaSessionCallback());
mediaSessionCompat.setActive(true);
}
public class mediaSessionCallback extends MediaSessionCompat.Callback {
#Override
public void onPlay() {
exoplayer.setPlayWhenReady(true);
}
#Override
public void onPause() {
exoplayer.setPlayWhenReady(false);
}
#Override
public void onSkipToPrevious() {
exoplayer.seekTo(0);
}
}
//Exo player methods
#Override
public void onTimelineChanged(Timeline timeline, Object manifest) {
}
#Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
#Override
public void onLoadingChanged(boolean isLoading) {
}
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if ((playbackState == exoplayer.STATE_READY) && playWhenReady) {
Log.d(TAG, "Player running");
} else if (playbackState == exoplayer.STATE_READY) {
Log.d(TAG, "paused");
}
}
#Override
public void onPlayerError(ExoPlaybackException error) {
}
#Override
public void onPositionDiscontinuity() {
}
//When Activity is been destroyed
#Override
protected void onDestroy() {
super.onDestroy();
releasePlayer();
}
}
Here is an option called same-name-layout-land.xml layout that you can handle your landscape situation and Android will take it and inflate automatically when your device rotated, with this you can manage how your Activity should be shown to the user, as long as this cool option exist, you just have to put your VideoPlayer xml tag with "match_parent" for height and width in landscape version of your xml layout.
UPDATE:
Of course, if you want to your video player to take whole of screen, you have to delete the default margin of it, and for making toolbar disappear you have to create two different styles.xml. Put one into res/values-port and the other into res/values-land, and in the landscape version you have to choose a *.NoActionBar version of your themes for it.

Android Progress Bar not working

I have made a progress bar in Android but it is not at all working. What am I doing wrong? I have just started to learn android. Below is my code.
MainActivity.java-
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
int mporgress=0;
EditText time;
private Handler mHandler = new Handler();
private static final int PROGRESS = 0x1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Startbuttononclick(View view){
Button startbutton=(Button) findViewById(R.id.button);
startbutton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
mporgress = Integer.parseInt(time.getText().toString());
progressBar.setProgress(mporgress);
new Thread(new Runnable() {
public void run() {
while (mporgress < 100) {
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(mporgress);
mporgress = doWork();
try{
Thread.sleep(1000);
}catch (InterruptedException e){}
}
});
}
}
}).start();
}
});
}
public void doprogress(View view) {
}
public int doWork(){
mporgress++;
return mporgress;
}
}
Activity_main.xml-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.android.progressbar.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the time:"
android:inputType="number"
android:id="#+id/editText" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="500dp"
android:layout_height="200dp"
android:id="#+id/progressBar4"
android:layout_gravity="center"
android:scrollbarSize="500dp" />
<Button
android:text="START"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_gravity="center"
android:onClick="Startbuttononclick" />
</LinearLayout>
Check out this image below-
- https://i.stack.imgur.com/2TBIb.png
Note- Enter the time in this image is an EditText with a hint and not a TextView.
It is appearing but not working.
I have done these changes so far.-
https://i.stack.imgur.com/UujpB.png
But still the progress bar is still continuously rotating.
Seems you forgot to initialize EditText -time and ProgressBar. Init it inside onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar =(ProgressBar) findViewById(R.id.progressBar4);
time = (EditText) findViewById(R.id.editText); //here
}
Also inside button click the bar:
progressBar = new ProgressBar(this);
mporgress = Integer.parseInt(time.getText().toString());
................
.............

Categories

Resources