其他分享
首页 > 其他分享> > android – Exoplayer不循环播放视频

android – Exoplayer不循环播放视频

作者:互联网

我在我的Android应用程序中使用Exoplayer来播放视频和音频文件.根据Exoplayer developer’s guide,以循环视频/音频,这是你必须要做的

MediaSource mediaSource = new ExtractorMediaSource(videoUri, ...);
// Loops the video indefinitely.
LoopingMediaSource loopingSource = new LoopingMediaSource(mediaSource);

所以我在Activity的onCreate方法中实现了这个

    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory factory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector = new DefaultTrackSelector(factory);

    simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this,trackSelector);
    simpleExoPlayer.setPlayWhenReady(true);
    simpleExoPlayerView.setPlayer(simpleExoPlayer);

    // Measures bandwidth during playback. Can be null if not required.
    bandwidthMeter2 = new DefaultBandwidthMeter();

// Produces DataSource instances through which media data is loaded.
    dataSourceFactory = new DefaultDataSourceFactory(this,
      Util.getUserAgent(this, applicationName), bandwidthMeter2);

// Produces Extractor instances for parsing the media data.
    extractorsFactory = new DefaultExtractorsFactory();
    mediaSource = new ExtractorMediaSource(videoUri,dataSourceFactory, extractorsFactory, null, null);

    loopingSource = new LoopingMediaSource(mediaSource);
    simpleExoPlayer.prepare(mediaSource);

但是我的视频文件的循环没有发生.它只播放一次.

解决方法:

我发现在Exoplayer开发人员指南中遗漏了一些重要信息.创建LoopingMediaSource实例后,不应该调用simpleExoPlayer.prepare(mediaSource);而是调用simpleExoPlayer.prepare(loopingSource);.下面是完整的代码

     BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory factory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector = new DefaultTrackSelector(factory);

    simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(this,trackSelector);
    simpleExoPlayer.setPlayWhenReady(true);
    simpleExoPlayerView.setPlayer(simpleExoPlayer);

    // Measures bandwidth during playback. Can be null if not required.
    bandwidthMeter2 = new DefaultBandwidthMeter();

// Produces DataSource instances through which media data is loaded.
    dataSourceFactory = new DefaultDataSourceFactory(this,
      Util.getUserAgent(this, applicationName), bandwidthMeter2);

// Produces Extractor instances for parsing the media data.
    extractorsFactory = new DefaultExtractorsFactory();

        MediaSource mediaSource = new ExtractorMediaSource(videoUri,dataSourceFactory, extractorsFactory, null, null);
    // Loops the video indefinitely.
    LoopingMediaSource loopingSource = new LoopingMediaSource(mediaSource);

    simpleExoPlayer.prepare(loopingSource);

标签:android,exoplayer
来源: https://codeday.me/bug/20190828/1752062.html