ExoPlayer stop/pause not working

NoNaMe picture NoNaMe · Jan 29, 2018 · Viewed 14.3k times · Source

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.

Answer

Naveen Dew picture Naveen Dew · Jan 29, 2018

try this...

if (exoPlayer != null) {
    exoPlayer.setPlayWhenReady(false);
    exoPlayer.stop();
    exoPlayer.seekTo(0);
}