使用exoplayer进行第二次播放不会显示视频窗口

时间:2019-07-10 21:42:36

标签: android surfaceview exoplayer

我有一个基于文件的简单播放实现,第一次运行良好。但是,当我再次实例化它时,我只是听到音频,而没有视频(只是黑屏)。感谢您的帮助

我研究了很多示例代码,但到目前为止没有任何帮助

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;
    }
}    

这是我的布局:     

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

</androidx.constraintlayout.widget.ConstraintLayout>

第二次播放只是显示一个黑色的窗口,背景中有音频

1 个答案:

答案 0 :(得分:0)

我的问题是删除视图,因此必须注释掉这一行:

mPlayerView.removeAllViews();

,我还决定保持玩家的活动状态,直到应用程序运行为止。这也避免了频繁的启动/结束活动调用。有了这一更改,我现在可以在音频和视频播放之间进行遍历了