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.:
@Override
public void onStart() {
// ...
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);
// ...
}
@Override
public void onStop() {
// ...
// 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:
@Override
protected void onPause() {
super.onPause();
if (player!=null) {
player.stop();
mLastPosition = player.getCurrentPosition();
}
}
@Override
protected void onResume() {
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
private void initiatePlayer() { try { exoPlayer = ExoPlayerFactory.newSimpleInstance(this); DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, this.getResources().getString(R.string.app_name))); DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory() .setMp4ExtractorFlags(Mp4Extractor.FLAG_WORKAROUND_IGNORE_EDIT_LISTS); ProgressiveMediaSource progressiveMediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory, extractorsFactory) .createMediaSource(videoUri); // playerView = new PlayerView(this); playerView.setPlayer(exoPlayer); exoPlayer.prepare(progressiveMediaSource); exoPlayer.setPlayWhenReady(true); PlayerControlView controlView = playerView.findViewById(R.id.exo_controller); mFullScreenIcon = controlView.findViewById(R.id.exo_fullscreen_icon); ImageView volumeControl = controlView.findViewById(R.id.exo_volume); mFullScreenIcon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(ORIENTATION ==Configuration.ORIENTATION_LANDSCAPE){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); }else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } Timer mRestoreOrientation = new Timer(); mRestoreOrientation.schedule(new TimerTask() { @Override public void run() { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } }, 2000); } }); volumeControl.setOnClickListener(view -> { float currentvolume = 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()); } } @Override protected void onPause() { super.onPause(); if (exoPlayer!=null) { exoPlayer.stop(); mLastPosition = exoPlayer.getCurrentPosition(); } } @Override protected void onResume() { 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
private void buildPlayer(String mp4Url)
add
player.seekTo(playbackPosition);
and then
@Override
public void onResume() {
super.onResume();
if(playbackPosition!=0 && player!=null){
player.seekTo(playbackPosition);
initializePlayer();
}
}
@Override
public void onPause() {
super.onPause();
player.stop();
if(player != null && player.getPlayWhenReady()) {
player.stop();
playbackPosition = player.getCurrentPosition();
player.setPlayWhenReady(true);
}
}
@Override
public void onStop() {
super.onStop();
player.setPlayWhenReady(false);
player.stop();
player.seekTo(0);
}
And you can try remove the
exoPlayer.setPlayWhenReady(true);
from
private void buildPlayer(String mp4Url)
Post a Comment for "How I Get Exoplayer To Resume A Video In My Activity's Onstart() Method?"