在Android的ExoPlayer中播放第二个视频后,音频仍在继续播放

时间:2020-10-23 17:59:16

标签: android exoplayer exoplayer2.x

我正在尝试使用android中的ExoPlayer播放本地视频,但是当我一个接一个地尝试两个视频时,第一个视频的音频继续播放而第二个视频没有播放,我该如何解决问题

SimpleExoPlayer exo;
 
public playVideo(Url url,String type){

    if (type.equals("VideoStart")) {
        Timber.i("VideoStart");

        File file = new File(url);
        Uri localUri = Uri.fromFile(file);

        exo = new SimpleExoPlayer.Builder(context).build();
        exo.setPlayWhenReady(true);
        playerView.setPlayer(exo);

        MediaSource mediaSource = buildMediaSource(localUri, context);
        exo.prepare(mediaSource, true, false);

    } else if (type.equals("VideoEnd")) {

        Timber.i("VideoEnd");
        if (exo != null) {
            Timber.i("ExoStopped");
            exo.setPlayWhenReady(false);
            exo.stop();
            exo.seekTo(0L);
            exo.release();
            exo = null;
        }
    }  
  }

private static MediaSource buildMediaSource(Uri uri, Context context) {
 return new ProgressiveMediaSource.Factory(new DefaultDataSourceFactory(context, Util.getUserAgent(context, "ExoPlayerDemo"))).createMediaSource(uri);

}

调用方法

playVideo(/storage/emulated/0/Android/data/com.exoplayer/files/Files/test.mp4,"VideoStart");
    
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        playVideo(/storage/emulated/0/Android/data/com.exoplayer/files/Files/test.mp4,"VideoEnd");
        playVideo(/storage/emulated/0/Android/data/com.exoplayer/files/Files/second.mp4,"VideoStart");
    }
}, 10000);

1 个答案:

答案 0 :(得分:2)

我怀疑您没有使用相同的SimpleExoPlayer,而是每次都在重新创建一个。

如果要一个接一个地播放多个视频,则需要使用playlist API

相关问题