ExoPlayer - play 10 files one after another

与世无争的帅哥 提交于 2019-12-03 09:47:51

问题


I have 10 video i need to play, once one is done, the next one starts to play.

I'm using Google's ExoPlayer, I use the example in the DEMO @ GitHub. I can play 1 video but if i try to play the next one, it wont start.

If i try to reInit the player, and the start playing again, it crashes.

private void loadvideo() {
    Uri uri = Uri.parse(VIDEO_LIBRARY_URL + currentVideo + ".mp4");
    sampleSource = new FrameworkSampleSource(this, uri, null, 2);

    // 1. Instantiate the player.
    // 2. Construct renderers.
    videoRenderer = new MediaCodecVideoTrackRenderer(sampleSource, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
    audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource);
    // 3. Inject the renderers through prepare.
    player.prepare(videoRenderer, audioRenderer);
    // 4. Pass the surface to the video renderer.
    surface = surfaceView.getHolder().getSurface();

    player.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface);
    // 5. Start playback.
    player.setPlayWhenReady(true);
    player.addListener(new ExoPlayer.Listener() {
        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            Log.d(TAG, "onPlayerStateChanged + " + playbackState);
            if (playbackState == ExoPlayer.STATE_ENDED) {
                currentVideo++;
                loadNextVideo();
            }
        }

        @Override
        public void onPlayWhenReadyCommitted() {

        }

        @Override
        public void onPlayerError(ExoPlaybackException error) {

        }
    });

}

What am i doing wrong? How can i play videos continuity?

Thanks.


回答1:


You can reuse the ExoPlayer up until the point that you call release(), and then it should no longer be used.

To change the media that it is currently playing, you essentially need to perform the following steps:

// ...enable autoplay...
player.stop();
player.seekTo(0L);
player.prepare(renderers);

Creating the renderers is a little bit more involved, but that's the flow you should follow and the player should be able to play back to back videos.




回答2:


I'm using Exoplayer change mp4 video success. I use the example in the DEMO. 1.DEMO project in DemoPlayer.java:

 private final RendererBuilder rendererBuilder;
 //remove final,then modify that:

 private RendererBuilder rendererBuilder;

 //and add the set method:
 public void setRendererBuilder(RendererBuilder rendererBuilder){
      this.rendererBuilder = rendererBuilder;
  }

 //finally,add stop method
 public void stop(){
      player.stop();
  }

2.DEMO project in PlayerActivity.java: add method:

private void changeVideo(){
        player.stop();
        player.seekTo(0L);
        //you must change your contentUri before invoke getRendererBuilder();
        player.setRendererBuilder(getRendererBuilder());
        player.prepare();
        playerNeedsPrepare = false;
    }

remember change param contentUri before invoke changeVideo method.




回答3:


OK, Answering my own question. on the example, google init the ExoPlayer at OnResume(). i had to re-init for every video like that:

player = ExoPlayer.Factory.newInstance(2, 1000, 5000);

if someone has a better idea, please let me know.




回答4:


There is another solution, you could refer to ConcatenatingMediaSource to achieve auto play next media.

In Demo App example : 1. Launch ExoPlayer 2. Select Playlists 3. Choose Cats->Dogs




回答5:


Use ConcatenatingMediaSource to play files in sequence. For example, for playing 2 media Uris (firstVideoUri and secondVideoUri), use this code:

MediaSource firstSource =
    new ExtractorMediaSource.Factory(...).createMediaSource(firstVideoUri);
MediaSource secondSource =
    new ExtractorMediaSource.Factory(...).createMediaSource(secondVideoUri);

ConcatenatingMediaSource concatenatedSource =
    new ConcatenatingMediaSource(firstSourceTwice, secondSource);

And then use concatenatedSource to play media files sequentially.



来源:https://stackoverflow.com/questions/28277091/exoplayer-play-10-files-one-after-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!