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.
You can use void setPlayWhenReady(boolean playWhenReady) . If Exo is ready, passing false will pause the player. Passing true will resume it. You can check the player's state using getPlaybackState() .
ExoPlayer is an app-level media player built on top of low-level media APIs in Android. It is an open source project used by Google apps, including YouTube and Google TV. ExoPlayer is highly customizable and extensible, making it capable of many advanced use cases.
To change the playback speed, call ExoPlayer. setPlaybackParameters passing in a PlaybackParameters instance with the required speed. As noted in the method's Javadoc, setting the playback parameters is not always possible.
try this...
if (exoPlayer != null) {
exoPlayer.setPlayWhenReady(false);
exoPlayer.stop();
exoPlayer.seekTo(0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With