How I Get Exoplayer To Resume A Video In My Activity's Onstart() Method?
Solution 1:
Currently you're calling release in onStop()
which will null out all the important pieces of the player, but not the exoPlayer
field (and destroy any state that you aren't keeping track of yourself).
There are a few different approaches. But no matter what you do, you'll likely want to keep track of some state yourself. Below I use them as fields, but they could also be placed in onSavedInstanceState()
. In onStop()
we're saving off two pieces of information and then pulling them out in onStart()
. 1) The last position our player was in when pausing and 2) whether we should play when resumed. You can likely move your seekTo
call out of the if == null
block since you'll probably always want to resume from where you left off.:
@OverridepublicvoidonStart() {
// ...if (exoPlayer == null) {
// init player
}
// Seek to the last position of the player.
exoPlayer.seekTo(mLastPosition);
// Put the player into the last state we were in.
exoPlayer.setPlayWhenReady(mPlayVideoWhenForegrounded);
// ...
}
@OverridepublicvoidonStop() {
// ...// Store off if we were playing so we know if we should start when we're foregrounded again.
mPlayVideoWhenForegrounded = exoPlayer.getPlayWhenReady();
// Store off the last position our player was in before we paused it.
mLastPosition = exoPlayer.getCurrentPosition();
// Pause the player
exoPlayer.setPlayWhenReady(false);
// ...
}
Now the other issue I see with your code sample is that exoPlayer.release()
won't null out the field exoPlayer
. So you could additionally add the line exoPlayer = null
after exoPlayer.release()
which should hopefully fix your issue of multiple exoPlayers. You could also move the release()
call to onDestroy()
but only if you know you're reinstantiating everything correctly.
Solution 2:
There is no need to reinit player again. The following code is pretty enough:
@OverrideprotectedvoidonPause() {
super.onPause();
if (player!=null) {
player.stop();
mLastPosition = player.getCurrentPosition();
}
}
@OverrideprotectedvoidonResume() {
super.onResume();
//initiatePlayer();if(mLastPosition!=0 && player!=null){
player.seekTo(mLastPosition);
}
}
Solution 3:
Try the following
- Get the player position in onPause.
initiate the palyer again in onResume and set seek to the last position of the player
privatevoidinitiatePlayer() { try { exoPlayer = ExoPlayerFactory.newSimpleInstance(this); DataSource.FactorydataSourceFactory=newDefaultDataSourceFactory(this, Util.getUserAgent(this, this.getResources().getString(R.string.app_name))); DefaultExtractorsFactoryextractorsFactory=newDefaultExtractorsFactory() .setMp4ExtractorFlags(Mp4Extractor.FLAG_WORKAROUND_IGNORE_EDIT_LISTS); ProgressiveMediaSourceprogressiveMediaSource=newProgressiveMediaSource.Factory(dataSourceFactory, extractorsFactory) .createMediaSource(videoUri); // playerView = new PlayerView(this); playerView.setPlayer(exoPlayer); exoPlayer.prepare(progressiveMediaSource); exoPlayer.setPlayWhenReady(true); PlayerControlViewcontrolView= playerView.findViewById(R.id.exo_controller); mFullScreenIcon = controlView.findViewById(R.id.exo_fullscreen_icon); ImageViewvolumeControl= controlView.findViewById(R.id.exo_volume); mFullScreenIcon.setOnClickListener(newView.OnClickListener() { @OverridepublicvoidonClick(View view) { if(ORIENTATION ==Configuration.ORIENTATION_LANDSCAPE){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } TimermRestoreOrientation=newTimer(); mRestoreOrientation.schedule(newTimerTask() { @Overridepublicvoidrun() { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } }, 2000); } }); volumeControl.setOnClickListener(view -> { floatcurrentvolume= exoPlayer.getVolume(); if (currentvolume > 0f) { previousVolume = currentvolume; exoPlayer.setVolume(0f); volumeControl.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_volume_off_white_24dp)); } else { exoPlayer.setVolume(previousVolume); volumeControl.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_volume_up_white_24dp)); } }); } catch (Exception e) { e.printStackTrace(); Log.d("MainExcep", e.getMessage()); } } @OverrideprotectedvoidonPause() { super.onPause(); if (exoPlayer!=null) { exoPlayer.stop(); mLastPosition = exoPlayer.getCurrentPosition(); } } @OverrideprotectedvoidonResume() { super.onResume(); initiatePlayer(); if(mLastPosition!=0){ exoPlayer.seekTo(mLastPosition); } }
Solution 4:
In my case, if i minimize or try to move from the this player video app to another app. The video player app always starting play from 0. I was try and succesfully, the video player app playing video from the last current position when you minimalize or move to another app, and this it.
go to
privatevoidbuildPlayer(String mp4Url)
add
player.seekTo(playbackPosition);
and then
@OverridepublicvoidonResume() {
super.onResume();
if(playbackPosition!=0 && player!=null){
player.seekTo(playbackPosition);
initializePlayer();
}
}
@OverridepublicvoidonPause() {
super.onPause();
player.stop();
if(player != null && player.getPlayWhenReady()) {
player.stop();
playbackPosition = player.getCurrentPosition();
player.setPlayWhenReady(true);
}
}
@OverridepublicvoidonStop() {
super.onStop();
player.setPlayWhenReady(false);
player.stop();
player.seekTo(0);
}
And you can try remove the
exoPlayer.setPlayWhenReady(true);
from
privatevoidbuildPlayer(String mp4Url)
Post a Comment for "How I Get Exoplayer To Resume A Video In My Activity's Onstart() Method?"