ExoPlayer:使用视频文件在后台播放声音

ExoPlayer: play sound in the background with a video file

正在播放视频,当应用进入后台时它应该继续播放音频,当我重新打开时它应该根据我们在音频中的位置恢复视频。

我在服务中使用 exoplayer,我能够在后台播放音频,但是当我对视频执行相同操作时,音频正在播放,但当我返回应用程序时,视频只是一个黑屏,如果我再次重复(进入后台并返回应用程序)该步骤,它将继续播放视频。

据我所知,exoplayer 正在缓冲下一帧,播放器视图卡住了,无法一次渲染所有帧。

我有一个 exoplayer 实例:

public class ExoPlayerWrapper {

private static SimpleExoPlayer exoPlayer;

public static SimpleExoPlayer getExoPlayer(Context context){

    if(exoPlayer == null){
        exoPlayer = ExoPlayerFactory.newSimpleInstance(context, new DefaultTrackSelector());
    }

    return exoPlayer;
}

public static void release() {
    if(exoPlayer != null) {
        exoPlayer.release();
        exoPlayer = null;
    }
}
}

一旦视图在前台,服务就会运行

最后,我在暂停集 playerView.setPlayer(null) 和恢复集 playerView.setPlayer(exoplayer_instance)

中做到了