【问题标题】:Audio is continue playing after second video is playing in ExoPlayer at android在 android 的 ExoPlayer 中播放第二个视频后,音频继续播放
【发布时间】:2020-10-23 17:59:16
【问题描述】:

我正在尝试在 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);

【问题讨论】:

    标签: android exoplayer exoplayer2.x


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      相关资源
      最近更新 更多