Search code examples
androidsurfaceviewexoplayer

Second playback with exoplayer displays no video window


I have a simple file based playback implementation that works fine the first time. But, when I instantiate it again, I just hear the audio, but no video (just a black screen). Any help is appreciated

I've looked into lot of sample code, but nothing has helped so far

private void initiateFilePlayback(Uri uri)
{
    closePlayer();

    mDataSourceFactory = new DefaultDataSourceFactory(mContext, mAppName);
    mExtractorsFactory = new DefaultExtractorsFactory();

    mExoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl());
    mExoPlayer.clearVideoSurface();

    mPlayerView = (PlayerView)findViewById(R.id.videoWindow);
    mPlayerView.setVisibility(View.VISIBLE);

    mPlayerView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
    | View.SYSTEM_UI_FLAG_IMMERSIVE
    | View.SYSTEM_UI_FLAG_FULLSCREEN
    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    mPlayerView.setPlayer(mExoPlayer);

    mMediaSource = new ExtractorMediaSource.Factory(mDataSourceFactory).setExtractorsFactory(mExtractorsFactory).createMediaSource(uri);

    mExoPlayer.prepare(mMediaSource);
    mExoPlayer.setPlayWhenReady(true);
}

private void closePlayer()
{
    if (mExoPlayer != null)
    {
        mPlayerView.removeAllViews();

        mExoPlayer.stop();
        mExoPlayer.release();
        mExoPlayer.removeMetadataOutput(null);

        mPlayerView.setVisibility(View.INVISIBLE | View.GONE);
        mPlayerView = null;

        mMediaSource = null;
        mDataSourceFactory = null;
        mExtractorsFactory = null;
        mExoPlayer = null;
    }
}    

And here's my layout:

<com.google.android.exoplayer2.ui.PlayerView
    android:id="@+id/videoWindow"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

The 2nd playback just shows a black window with audio in the background


Solution

  • My issue was removing the view, so had to comment out this line:

    mPlayerView.removeAllViews();
    

    and I also decided to keep my player activity alive till the app is running. This also avoided frequent start/finish activity calls. With this change, I'm now able to traverse between audio & video playback with no issues