Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exoplayer - How to prevent rebuffering when it's on Repeat mode?

I'm using exoplayer 2, and have Repeat mode on, meaning it will keep playing the video once it reaches the end. However, after the video ends and plays again, it keeps rebuffering (reloading the video). How do I prevent this from happening?

player = new SimpleExoPlayer.Builder(mContext).build();
player.setPlayWhenReady(false);
player.setRepeatMode(Player.REPEAT_MODE_ONE);

Edit1:

Have tried using LoopingMediaSource, not sure if I am doing it correctly though? Because it still rebuffers when it automatically replays the video.

DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext,
                Util.getUserAgent(mContext, "yourApplicationName"));
        Uri uri = Uri.parse(mUrl);

        MediaSource videoSource =
                new ProgressiveMediaSource.Factory(dataSourceFactory)
                        .createMediaSource(uri);
        LoopingMediaSource loopingMediaSource = new LoopingMediaSource(videoSource);
        player.prepare(loopingMediaSource);
like image 709
DIRTY DAVE Avatar asked Oct 05 '20 15:10

DIRTY DAVE


People also ask

What is buffering in ExoPlayer?

Buffering occurs in video streaming when the software downloads a particular amount of data before beginning to play the video. While the next section of the file downloads in the background, you may stream the data that has already been preloaded and stored in the buffer. ExoPlayer is a library developed by Google.

What version of ExoPlayer has repeat mode?

Starting with version 2.5, ExoPlayer… | by Toni Heidenreich | google-exoplayer | Medium Starting with version 2.5, ExoPlayer will have a repeat mode feature allowing you to seamlessly switch between regular playback, Repeat One, and Repeat All modes.

What is ExoPlayer and how to use it?

While the next section of the file downloads in the background, you may stream the data that has already been preloaded and stored in the buffer. ExoPlayer is a library developed by Google. It provides an alternative to Android’s MediaPlayer API for playing audio and video both locally and over the Internet.

How do I get a playback error in ExoPlayer?

Errors that cause playback to fail can be received by implementing onPlayerError (PlaybackException error) in a registered Player.Listener. When a failure occurs, this method will be called immediately before the playback state transitions to Player.STATE_IDLE . Failed or stopped playbacks can be retried by calling ExoPlayer.retry.


1 Answers

You need cache data in ExoPlayer

  • Create a custom cache data source factory
public class CacheDataSourceFactory implements DataSource.Factory {
    private final Context context;
    private final DefaultDataSourceFactory defaultDatasourceFactory;
    private final long maxFileSize, maxCacheSize;

    public CacheDataSourceFactory(Context context, long maxCacheSize, long maxFileSize) {
        super();
        this.context = context;
        this.maxCacheSize = maxCacheSize;
        this.maxFileSize = maxFileSize;
        String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name));
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        defaultDatasourceFactory = new DefaultDataSourceFactory(this.context,
                bandwidthMeter,
                new DefaultHttpDataSourceFactory(userAgent, bandwidthMeter));
    }

    @Override
    public DataSource createDataSource() {
        LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize);
        SimpleCache simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor);
        return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(),
                new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize),
                CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null);
    }
}
  • And the player
    BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    TrackSelection.Factory videoTrackSelectionFactory =
            new AdaptiveTrackSelection.Factory(bandwidthMeter);
    TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
    
    SimpleExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
    MediaSource audioSource = new ExtractorMediaSource(Uri.parse(url),
                new CacheDataSourceFactory(context, 100 * 1024 * 1024, 5 * 1024 * 1024), new DefaultExtractorsFactory(), null, null);
    exoPlayer.setPlayWhenReady(true); 
    exoPlayer.prepare(audioSource);
like image 196
AAV Avatar answered Nov 15 '22 04:11

AAV