Exoplayer 2更改媒体源以剪辑视频

时间:2018-11-16 11:53:14

标签: android exoplayer exoplayer2.x

我正在尝试为我们的一个应用程序实现exoplayer 2。

看起来像文档和示例的东西比自动播放更复杂。

我的问题是我创建了一个媒体源并将其设置为播放器,此后,我有一个自定义图标来播放视频,完成后,我将其恢复为0。

我还有一个自定义的双重搜索栏,当移动它时它将搜索到一个位置并将视频剪辑在选定的开始时间和结束时间之间。

我不确定是否可以更改播放器的来源而不重新创建整个游戏。

我将播放器初始化为:

@AfterViews
protected void init() {
        TrackSelector trackSelector = new DefaultTrackSelector();
        mPlayer = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector);
        mVideoView.setPlayer(mPlayer);
        mPlayer.addListener(this);
}

然后,我准备像这样的播放器:

public void setVideoPath(final String videoPath) {
        mVideoPath = videoPath;
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getContext(), Util.getUserAgent(getContext(), "yourApplicationName"));
        // This is the MediaSource representing the media to be played.
        videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(videoPath));
        mPlayer.prepare(videoSource);
}

点击我的播放图标后,它将运行:

@Click(R.id.view_video_preview_container_play)
    void onPlayClicked() {
        mPlayButton.setVisibility(GONE);
        mPlayer.setPlayWhenReady(true);
}

播放完成后,我会做

@Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

        if (playbackState == STATE_ENDED) {
            mPlayButton.setVisibility(VISIBLE);
            mPlayer.seekTo(0);
            mPlayer.setPlayWhenReady(false);
        }
}

不,当我寻求时:

disposable = source
                .debounce(500, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(range -> {
                            mPlayer.stop();
                            mPlayButton.setVisibility(VISIBLE);
                            clippingSource = new ClippingMediaSource(videoSource, 2000000, 5000000);
                            mPlayer.prepare(clippingSource);
                        }
                );

现在,当源发出我的观察到的东西时,我会回来:

2018-11-16 12:48:16.463 28090-28209/nl.dtt.vormats E/ExoPlayerImplInternal: Source error.
    com.google.android.exoplayer2.source.ClippingMediaSource$IllegalClippingException: Illegal clipping: not seekable to start
        at com.google.android.exoplayer2.source.ClippingMediaSource$ClippingTimeline.<init>(ClippingMediaSource.java:350)
        at com.google.android.exoplayer2.source.ClippingMediaSource.refreshClippedTimeline(ClippingMediaSource.java:296)
        at com.google.android.exoplayer2.source.ClippingMediaSource.onChildSourceInfoRefreshed(ClippingMediaSource.java:262)
        at com.google.android.exoplayer2.source.ClippingMediaSource.onChildSourceInfoRefreshed(ClippingMediaSource.java:34)
        at com.google.android.exoplayer2.source.CompositeMediaSource$1.onSourceInfoRefreshed(CompositeMediaSource.java:103)
        at com.google.android.exoplayer2.source.BaseMediaSource.refreshSourceInfo(BaseMediaSource.java:73)
        at com.google.android.exoplayer2.source.ExtractorMediaSource.notifySourceInfoRefreshed(ExtractorMediaSource.java:400)
        at com.google.android.exoplayer2.source.ExtractorMediaSource.prepareSourceInternal(ExtractorMediaSource.java:348)
        at com.google.android.exoplayer2.source.BaseMediaSource.prepareSource(BaseMediaSource.java:137)
        at com.google.android.exoplayer2.source.CompositeMediaSource.prepareChildSource(CompositeMediaSource.java:109)
        at com.google.android.exoplayer2.source.ClippingMediaSource.prepareSourceInternal(ClippingMediaSource.java:216)
        at com.google.android.exoplayer2.source.BaseMediaSource.prepareSource(BaseMediaSource.java:137)
        at com.google.android.exoplayer2.ExoPlayerImplInternal.prepareInternal(ExoPlayerImplInternal.java:396)
        at com.google.android.exoplayer2.ExoPlayerImplInternal.handleMessage(ExoPlayerImplInternal.java:286)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:166)
        at android.os.HandlerThread.run(HandlerThread.java:65)

不幸的是,正如我在文档前所说的那样,如果非常糟糕,我只能发现在以下位置抛出了异常:

if (startUs != 0 && !window.isSeekable) {
          throw new IllegalClippingException(IllegalClippingException.REASON_NOT_SEEKABLE_TO_START);
}

特别是window.isSeekable为false。

1 个答案:

答案 0 :(得分:2)

解决方案是重新创建MediaSource,因为似乎无法重复使用同一MediaSource。