7

I don't know why, but sometimes Exoplayer buffers my video very slowly. My server is responding properly and the internet is also fast but sometimes Exoplayer buffers my video slowly for less than 1 second. And it buffering always after every 1-2 seconds on playing.

        int MIN_BUFFER_DURATION = 3000;
        int MAX_BUFFER_DURATION = 8000;
        int MIN_PLAYBACK_RESUME_BUFFER = 1500;
        int MIN_PLAYBACK_START_BUFFER = 500;
        LoadControl loadControl = new DefaultLoadControl.Builder()
                .setAllocator(new DefaultAllocator(true, 16))
                .setBufferDurationsMs(MIN_BUFFER_DURATION,
                        MAX_BUFFER_DURATION,
                        MIN_PLAYBACK_START_BUFFER,
                        MIN_PLAYBACK_RESUME_BUFFER)
                .setTargetBufferBytes(-1)
                .setPrioritizeTimeOverSizeThresholds(true).createDefaultLoadControl();
        TrackSelector trackSelector = new DefaultTrackSelector();
        simpleExoPlayer = new ExoPlayer.Builder(this).setTrackSelector(trackSelector).setLoadControl(loadControl).build();
        binding.exoPlayerView.setPlayer(simpleExoPlayer);
        mediaItem = MediaItem.fromUri(getVid);
        simpleExoPlayer.addMediaItem(mediaItem);
        simpleExoPlayer.prepare();
        simpleExoPlayer.play();

I'm testing my video in my Exoplayer and Chrome Browser player. Chrome browserplayer plays my video 4X faster than my appExoplayer`? And I'm playing the same video and the same time. Someone also asked this question in exoplayer git but not got a good answer or result see their question exoplayer issue github this same issue causing me!

Does anyone know why this happens? Your answer will helpful for me.

M DEV
  • 763
  • 1
  • 7
  • 20
  • 1
    Update .setTargetBufferBytes(C.LENGTH_UNSET) – Prasad Tamgale May 12 '22 at 08:41
  • @PrasadTamgale sir please explain what you wrote. I don't understand? – M DEV May 12 '22 at 11:21
  • I was saying, maybe try setting the target buffer bytes size to C.LENGTH_UNSET & see if that helps. – Prasad Tamgale May 12 '22 at 11:26
  • @PrasadTamgale can you explain to me how to implement this? with some code? – M DEV May 12 '22 at 11:27
  • Same code of yours, just update from .setTargetBufferBytes(-1) to .setTargetBufferBytes(C.LENGTH_UNSET) – Prasad Tamgale May 12 '22 at 11:56
  • @PrasadTamgale does not work! The problem is the same. – M DEV May 12 '22 at 12:31
  • @PrasadTamgale any other solution?? – M DEV May 12 '22 at 12:34
  • I don't see anything wrong but the set values for the parameters might be causing the scenario which you are mentioning. – Prasad Tamgale May 12 '22 at 16:05
  • Let's do few tweaks and check? Make DefaultAllocator(true,1024) & also .setTargetBufferBytes(1024) – Prasad Tamgale May 12 '22 at 16:13
  • @PrasadTamgale let me explain. This problem is happening on 30-second video and size is ~6MB and this problem is not happening on 30-second video and size is ~3MB. and this problem happens ~8 times on 10 times play. Do you know why this problem is happening? – M DEV May 12 '22 at 17:32
  • `int MIN_PLAYBACK_RESUME_BUFFER = 1500; int MIN_PLAYBACK_START_BUFFER = 500;` this is the problem @MDev. They together become 2000 which is 2 seconds. I increased the values and then, the buffer happens for a longer time. Actually, why do you need that? Any use case for min 3 seconds loading? – Sambhav Khandelwal May 16 '22 at 04:21
  • @Sambhav.K Because I want to load my video fastly on startup time. So, I added minimum value there. Anything else I'm doing? – M DEV May 16 '22 at 05:33

3 Answers3

3
  1. Make sure you are using the latest version of Exoplayer. As of this writing, that is 2.10.4.

  2. Try increasing the buffer duration values in your LoadControl:

int MIN_BUFFER_DURATION = 3000; // 3 seconds 
int MAX_BUFFER_DURATION = 8000; // 8 seconds 
int MIN_PLAYBACK_RESUME_BUFFER = 1500; // 1.5 seconds 
int MIN_PLAYBACK_START_BUFFER = 500; // 0.5 seconds 
LoadControl loadControl = new DefaultLoadControl.Builder() 
   .setAllocator(new DefaultAllocator(true, 16)) 
   .setBufferDurationsMs(
        MIN_BUFFER_DURATION, 
        MAX_BUFFER_DURATION, 
        MIN_PLAYBACK_START_BUFFER, 
        MIN_PLAYBACK_RESUME_BUFFER) 
   .setTargetBufferBytes(-1) 
   .setPrioritizeTimeOverSizeThresholds(true)
   .createDefaultLoadControl()
  1. Try using a different LoadControl. For example, you could use DefaultLoadControl with a smaller target buffer (e.g. 25% of the video bitrate):
int TARGET_BUFFER_BYTES = (int) (0.25 * videoBitrate); // 25% of the video bitrate in bytes 
LoadControl loadControl = new DefaultLoadControl(
    new DefaultAllocator(true, 16), 
    TARGET_BUFFER_BYTES, 
    DEFAULT_MIN_REBUFFER_MS,
    DEFAULT_MAX_LOADING_MS,
    DEFAULT_MIN_ELAPSED_MS_BEFORE_STOPPING,
    false
); 
  1. Try using a different Allocator. For example, you could use a larger one:
int allocatorSize = 2 * 1024 * 1024; // 2MB 
Allocator allocator = new DefaultAllocator(true, allocatorSize); 
LoadControl loadControl = new DefaultLoadControl(
    allocator,
    DEFAULT_TARGET_BUFFER_BYTES, 
    DEFAULT_MIN_REBUFFER_MS,
    DEFAULT_MAX_LOADING_MS,
    DEFAULT_MIN_ELAPSED_MS_BEFORE_STOPPING,
    false
); 
hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58
Carter McKay
  • 432
  • 2
  • 12
  • sir, how can I get bitrate of my video? – M DEV May 18 '22 at 17:50
  • You can get the bitrate of your video by using ExoPlayer.TrackInfo.bitrate ``` int videoBitrate = exoPlayer.getTrackInfo().bitrate; ``` Note: If you are using ExoPlayer.EventListener to listen for events, make sure you don't have a lot of logic in your event listeners. Because they are called on the main thread, they can slow down the playback if they take too long. – Carter McKay May 18 '22 at 17:57
  • I tried your code but some of the code not found in the Exoplayer library like these are `DEFAULT MAX LOADING MS , DEFAULT MIN ELAPSED MS BEFORE STOPPING,` not found in exoplayer library. I tried 2nd method but not found good result. I'm testing my video in my Exoplayer and `Chrome Browser` player. Chrome browser player plays my video 4X faster then my app Exoplayer? And I'm playing my same video and same time. Someone also asked this question in exoplayer git but not got good answer or result see their question https://github.com/google/ExoPlayer/issues/6484 this same issue causing with me! – M DEV May 18 '22 at 18:33
  • I just edited the code for the 1st method, it may fix it – Carter McKay May 18 '22 at 18:40
  • I appreciate your effort and am thankful for it. But I could not find the result. I think this is a bug in Exoplaye? – M DEV May 18 '22 at 18:55
  • I think thats very possible – Carter McKay May 18 '22 at 18:57
  • yeah, the same video I played with `1080px` on youtube by uploading and playing without loading and I also played my video on `Chrome Browser` by uploading on my server and it plays after loading ~0.5 seconds, and when the same video play in-app it play after 4-6 second and buffering also in playing time??? – M DEV May 18 '22 at 19:05
  • That is so weird – Carter McKay May 18 '22 at 19:10
  • 1
    It's working very fine(dep. update of Exo or any bug fixing). But your answer is helpful in improving playing speed and video loading speed. Thank you – M DEV Sep 06 '22 at 08:41
  • So how did you fix it ?? Can you describe more ? – Malo Mar 15 '23 at 07:09
0

set the following to 0 -> test behavior -> make adjustments if needed

int MIN_PLAYBACK_RESUME_BUFFER = 1500;
int MIN_PLAYBACK_START_BUFFER = 500;
orimen
  • 571
  • 4
  • 11
  • Thanks for your answer. I tried to make them 0 but the video is buffering every time means buffering and playing buffering and playing. Do you have any other solutions or changes? – M DEV May 17 '22 at 17:10
-1

What worked for me was this:

    int MIN_BUFFER_DURATION = 3000; // 3 seconds
    int MAX_BUFFER_DURATION = 3000; // 3 seconds
    int MIN_PLAYBACK_RESUME_BUFFER = 1500; // 1.5 seconds
    int MIN_PLAYBACK_START_BUFFER = 500; // 0.5 seconds
    LoadControl loadControl = new DefaultLoadControl.Builder() .setAllocator(new DefaultAllocator(true, 16)) .setBufferDurationsMs(MIN_BUFFER_DURATION, MAX_BUFFER_DURATION, MIN_PLAYBACK_START_BUFFER, MIN_PLAYBACK_RESUME_BUFFER) .setTargetBufferBytes(-1) .setPrioritizeTimeOverSizeThresholds(true).createDefaultLoadControl();
    ExoPlayer player= new ExoPlayer.Builder(this).setLoadControl(loadControl).build();

as per @Carter McKay answer just changed the MAX_BUFFER_DURATION.

Fahad Alkamli
  • 132
  • 2
  • 10