学而实习之 不亦乐乎

Android 中 ExoPlayer 播放器视频缓冲慢的问题

2023-09-13 14:01:39

Exoplayer缓冲视频非常慢,但服务器响应正常,互联网访问也很快,但在 Exoplayer 中播放时缓冲视频比较慢。

原始代码 

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

修改后

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