Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExoPlayer - play 10 files one after another

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.

like image 337
Shahar Avatar asked Feb 02 '15 11:02

Shahar


People also ask

How do you play the next ExoPlayer video?

1 Answer. Show activity on this post. 3] Just check by clicking next button from the media controller if that works then you are done, now the videos will be played automatically once finished the current one.

Does YouTube use ExoPlayer?

Screenshot: The YouTube Android app, which uses ExoPlayer as its video player. ExoPlayer is an app-level media player built on top of low-level media APIs in Android. It is an open source project used by Google apps, including YouTube and Google TV.

How does ExoPlayer work on Android?

ExoPlayer is a media player library that provides a way to play audio and video with lots of customization in it. It is an alternative that is used to play videos and audios in Android along with MediaPlayer. ExoPlayer is a library that is the best alternative source for playing audio and videos on Android.

How do you play ExoPlayer?

Add dependency of ExoPlayer in your application. Add ExoPlayer in your layout file. In your Java/Kotlin file make a function to initialize the player. This function will initialise the ExoPlayer in the player view and it will play the media when the player is ready.


2 Answers

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.

like image 75
Malwinder Singh Avatar answered Nov 09 '22 00:11

Malwinder Singh


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.

like image 20
Lo-Tan Avatar answered Nov 09 '22 00:11

Lo-Tan