Exoplayer不循环播放视频

问题描述 投票:4回答:4

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

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

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

android exoplayer
4个回答
4
投票

我发现在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);

1
投票
player = ExoPlayerFactory.newSimpleInstance(this);

上面的代码行使用默认轨道选择器实例化ExoPlayer。如果我们不自定义轨道选择器,我们可以避免创建带宽计,轨道选择器。

使用LoopingMediaSource可以实现循环。我们必须在LoopingMediaSouce构造函数中传递循环计数。如果我们想要无限期地循环媒体,最好使用播放器的setRepeatMode()方法。

new LoopingMediaSource(mediaSource, 3)

附上样品供参考。

<code>
    private MediaSource getLoopingMediaSource() {
        DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(ExoPlayerActivity.this, Util.getUserAgent(ExoPlayerActivity.this, "application-name"));
        MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Samples.MP4_URI);
        LoopingMediaSource loopingMediaSource = new LoopingMediaSource(mediaSource, 3);
        return  loopingMediaSource;
        }

    player = ExoPlayerFactory.newSimpleInstance(this);
    playerView.setPlayer(player);
    player.prepare(getLoopingMediaSource());
    player.setPlayWhenReady(true);
</code>

0
投票

对我有用的唯一解决方案是:

    simpleExoPlayer.prepare(mediaSource);
    simpleExoPlayer.setPlayWhenReady(true);
    simpleExoPlayer.seekTo(0);
    exoPlayer.requestFocus();
    simpleExoPlayer.addListener(new Player.EventListener() {
        @Override
        public void onLoadingChanged(boolean isLoading) {
        }

        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            switch (playbackState) {
                case Player.STATE_READY:
                    prog.setVisibility(View.GONE);
                    simpleExoPlayer.setPlayWhenReady(true);
                    break;
                case Player.STATE_BUFFERING:
                    prog.setVisibility(View.VISIBLE);
                    simpleExoPlayer.seekTo(0);
                    break;
                case Player.STATE_IDLE:
                    simpleExoPlayer.retry();
                    break;
            }
        }
    });
}

0
投票

您只需将重复模式设置为“Repeat_MODE_ONE”即可。

例如:

  val player: SimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(
        context,
        DefaultRenderersFactory(context),
        DefaultTrackSelector(),
        DefaultLoadControl()
    ).apply {


       repeatMode = Player.REPEAT_MODE_ONE

    }

p.s.播放器来自com.google.android.exoplayer2

© www.soinside.com 2019 - 2024. All rights reserved.