Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExoPlayer stop/pause not working

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.

like image 845
NoNaMe Avatar asked Jan 29 '18 17:01

NoNaMe


People also ask

How do you pause exo player?

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() .

What is the use of ExoPlayer?

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.

How do I change playback speed in ExoPlayer?

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.


1 Answers

try this...

if (exoPlayer != null) {
    exoPlayer.setPlayWhenReady(false);
    exoPlayer.stop();
    exoPlayer.seekTo(0);
}
like image 183
Naveen Dew Avatar answered Sep 30 '22 00:09

Naveen Dew