Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExoPlayer2 - How to release from fragment

I have one activity with 2 fragments (not a ViewPager) & want to use ExoPlayer2 in one of the fragments to play content. The initial fragment shows a list of content & when clicked the second fragment is shown to play the selected content. However when I click back to the initial fragment I can still hear the content playing; it's as though the second fragment is still open. I'm (trying to) release the player this way:

@Override
public void onStop() {
    super.onStop();
    releasePlayer();
}

public void releasePlayer() {

    if (player != null) {
        player.setPlayWhenReady(true);
        player.stop();
        player.release();
        player = null;
        trackSelector = null;
        simpleExoPlayerView.setPlayer(null);
        simpleExoPlayerView = null;
        System.out.println("releasePlayer");
    }
}

I read this but it didn't provide much of a solution. Also, read several posts where ExoPlayer is used in ViewPager fragments but not exactly sure how those examples released the player. Followed the releasePlayer() method from the demo app found here but no luck. Called releasePlayer() from onPause(), onDetach(), onDestroyView() but it made no difference.

Here's some logging set up after the listener is added to the player but don't see anything to explain why it's not releasing...

07-05 19:01:07.216 2529-2643/? V/RenderScript: 0xe0494000 Launching thread(s), CPUs 4
07-05 19:01:11.871 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:11.876 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onTimelineChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onLoadingChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onTimelineChanged...
07-05 19:01:11.904 3064-3064/? V/MediaCodecInfo: Listener-onLoadingChanged...
07-05 19:01:12.467 3064-3064/? V/MediaCodecInfo: Listener-onTimelineChanged...
07-05 19:01:12.468 3064-3064/? V/MediaCodecInfo: Listener-onTimelineChanged...
07-05 19:01:12.515 3064-3099/? V/SoftFFmpegVideo: got OMX_IndexParamPortDefinition, width: 320, height: 180
07-05 19:01:12.708 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:12.708 3064-3064/? V/MediaCodecInfo: Listener-onPlayerStateChanged...
07-05 19:01:13.380 3064-3064/? V/MediaCodecInfo: Listener-onLoadingChanged...
07-05 19:01:13.655 3064-3064/? V/MediaCodecInfo: Listener-onLoadingChanged...
07-05 19:01:18.528 3064-3099/? V/SoftFFmpegVideo: ~SoftFFmpegVideo

Tried the button click but it failed

fragmentExoBinding.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            System.out.println("button click");
            releasePlayer();
        }
    });

It was crashing everytime I shut it down (using back press, home or recent) b/c there's no longer a player. Updated the releasePlayer() method with if (player != null) but can still hear the content playing even after button click.

like image 888
JC23 Avatar asked Jul 05 '17 20:07

JC23


1 Answers

I am using ExoPlayer inside the Activity and I am using a two-step process. onPause I am stopping the playback:

private SimpleExoPlayer player;     
@Override
protected void onPause() {
    super.onPause();

    // Pause video if it's playing
    if (isVideoPlaying()) {
        if (player != null)
            player.setPlayWhenReady(false);
    }
}

And onDestroy I release the player

@Override
protected void onDestroy() {
    releasePlayer();
    super.onDestroy();
}

private void releasePlayer() {
        if (player != null) {
            player.stop();
            player.release();
            player = null;
        }
    }
like image 75
Kirk_hehe Avatar answered Nov 08 '22 02:11

Kirk_hehe