Reputation: 6222
I have added ExoPlayer in one of the activity to play videos and it is working properly. But when the video is playing and user presses back button, the video still continue to play in background. I want it to be stopped. I tried capturing the back button and tried pausing the video but it is not working. Unless you kill the activity, the video keeps on playing until it is finished. Is it a bug or this is how the library is supposed to work?
Here is working code of expPlayr
String url = getIntent().getStringExtra("videoURL");
String title = getIntent().getStringExtra("title");
exoPlayerView = (SimpleExoPlayerView) findViewById(R.id.exo_player_id);
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory(title);
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource m = new HlsMediaSource(Uri.parse(url), dataSourceFactory, null,null);
exoPlayerView.setPlayer(exoPlayer);
exoPlayer.prepare(m);
exoPlayer.setPlayWhenReady(true);
Here is the code I'm trying to use to stop the video.
@Override
public void onBackPressed(){
try{
exoPlayer.stop();
}catch(Exception e){
e.printStackTrace();
}
}
I review the code in debug mode and this statement is executed, but still not stopping the video.
Upvotes: 12
Views: 27168
Reputation: 164
I just ran into this problem and none of the solutions here helped. Hope it helps someone.
So, what else could be wrong? Calling exoPlayer.play()
in Player.STATE_READY
is the programmatical error.
Remove exoPlayer.play()
from the callBack.
If you want to play the video file immediately the player is ready, create a flag at Player.STATE_READY
and use a handler to check if it is ready, then play.
I hope this helps.
Upvotes: 0
Reputation: 231
This is an example how you can implement what Naveen Dew said.
@Override
public void onDestroyView() {
super.onDestroyView();
if (exoPlayer != null) {
exoPlayer.setPlayWhenReady(false);
exoPlayer.stop();
exoPlayer.seekTo(0);
}
}
Upvotes: 1
Reputation: 482
Following code works fine for me:
@Override
public void onPause() {
super.onPause()
exoPlayer.setPlayWhenReady(false);
}
@Override
public void onResume() {
super.onResume()
exoPlayer.setPlayWhenReady(true);
}
Upvotes: 1
Reputation: 1489
I also went through same issue in my project. The issue I had was - Exoplayer is initialized twice when screen is loaded and one instance is released when we call realeseExoplayer() in onPause or onResume() as per SDK version. The second instance continues to play in the background even when activity is closed. So request to double check whether Exoplayer is initialized twice or not. If it is initialized more than once, make necessary changes to initialize it once.
Upvotes: 7
Reputation: 4247
My code is working for audio activity:
public void release() {
if (exoPlayer != null) {
exoPlayer.removeListener(eventListener);
exoPlayer.release();
exoPlayer = null;
dataSpec =null;
assetDataSource = null;
factory = null;
audioSource =null;
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
setPlayPause(false);
release();
finish();
}
Upvotes: 5
Reputation: 2789
I'd recommend to initialize and release the player in life-cycle methods of the Activity. If you do this properly you don't need the onBackPressed method, because your code is executed in the lifecycle methods.
The PlayerActivity of the ExoPlayer demo application on Github has essentially these lifecycle methods:
@Override
public void onStart() {
super.onStart();
if (Util.SDK_INT > 23) {
initializePlayer();
}
}
@Override
public void onResume() {
super.onResume();
if (Util.SDK_INT <= 23 || player == null) {
initializePlayer();
}
}
@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT <= 23) {
releasePlayer();
}
}
@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT > 23) {
releasePlayer();
}
}
Upvotes: 2
Reputation: 620
You can use the example of exoplayer, releasePlayer method:
private void releasePlayer() {
if (player != null) {
updateResumePosition();
player.removeListener(this);
player.stop();
player.release();
player = null;
trackSelector = null;
trackSelectionHelper = null;
eventLogger = null;
}
}
See PlayerActivity class in ExoPlayer demo.
Upvotes: 1
Reputation: 1295
try this...
if (exoPlayer != null) {
exoPlayer.setPlayWhenReady(false);
exoPlayer.stop();
exoPlayer.seekTo(0);
}
Upvotes: 11