Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ExoPlayer start playing as soon as possible without too much buffering

I am using a simple exoplayer test code to play videos from an URL and I noticed that after the player connects, it buffers about 2-3 seconds until it starts playing (I am referring to approximately 2-3 seconds because I do not know exactly how it works internally, it may be a certain number of bytes). Giving the fact that it takes about 1-2 seconds to connect (then to start buffering, then effectively start the video) it takes about 4 seconds to start the video, which seems like too much. I tested with a 3 seconds video, 640x480, 1.23MB in size, on a connection of detected speed ~24Mbits/s using a speed test app).

Is there a way to speed up the startup of the video (for example, by setting the initial buffering size needed to start)?

This is my testing code:

TrackSelector trackSelector = new DefaultTrackSelector();
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);

PlayerView playerView = findViewById(R.id.player_view);
playerView.setPlayer(player);
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
            Util.getUserAgent(this, "simpleAudioApp"));
dataSourceFactory = new CacheDataSourceFactory(getSimpleCacheInstance(),
            dataSourceFactory);

Uri audioSourceUri = Uri.parse("http://...somevideo.mp4");
MediaSource audioSource = new ExtractorMediaSource.Factory(dataSourceFactory)
            .createMediaSource(audioSourceUri);
LoopingMediaSource loopingSource = new LoopingMediaSource(audioSource, 1);

// Prepare the player with the source.
player.prepare(loopingSource);
player.setPlayWhenReady(true);
player.addListener(new Player.EventListener() {
        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            if (playbackState == Player.STATE_ENDED) {
                Toast.makeText(TestActivity2.this, "Video ended", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(TestActivity2.this, "video state chnged: " + playbackState, Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onRepeatModeChanged(int repeatMode) {
            Toast.makeText(TestActivity2.this, "repeat chnged: " + repeatMode, Toast.LENGTH_LONG).show();
        }
    });

Is there any way of achieving this?

like image 502
Andrei F Avatar asked Jan 07 '19 11:01

Andrei F


1 Answers

You can use custom loadcontrol with this pattern:

DefaultLoadControl loadControl = new DefaultLoadControl
.Builder()
.setBufferDurationsMs(minBufferMS, maxBufferMs, bufferForPlayackMS, bufferForPlaybackAfterRebufferMs)
.createDefaultLoadControl();

Sample usage:

TrackSelector trackSelector = new DefaultTrackSelector();
    
DefaultLoadControl loadControl = new DefaultLoadControl
.Builder()
.setBufferDurationsMs(32*1024, 64*1024, 1024, 1024)
.createDefaultLoadControl();
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
like image 166
Asuk Nath Avatar answered Nov 03 '22 15:11

Asuk Nath