Search code examples
androidexoplayer

Exoplayer not looping the video


I'm using Exoplayer in my android app to play video and audio files. according to Exoplayer developer's guide,in order to loop a video/audio, this is what you have to do

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

So I implemented it like this in my Activity's onCreate method

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

But looping of my video file is not taking place. it just plays once.


Solution

  • I discovered that there was some crucial info left out in Exoplayer developers guide. after creating LoopingMediaSource instance, one should not call simpleExoPlayer.prepare(mediaSource); but instead call simpleExoPlayer.prepare(loopingSource);. below is the complete code

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