ExoPlayer实现4G网络下暂停缓存功能
ExoPlayer
项目地址:https://gitcode.com/gh_mirrors/ex/ExoPlayer
免费下载资源
·
最近接到一个需求,要求4G网络下播放器不仅要暂停而且要暂停缓存功能,研究了半天源码功力不够就问了下度娘,果然,某位前辈已经研究出解决秘方了,参考秘方,本姑娘稍加修炼遍解决了此问题,此处一个(* ̄︶ ̄)。
使用ExoPlayer播放视频时,prepare一个视频资源后,ExoPlayer就会自动进行缓冲,但是有需求是当前是移动网络就停止播放并停止缓冲,停止播放简单就调用setplayerstatewhenready就行了,但并没有找到停止缓冲的相关方法。后面去看源码发现有个LoadControl的接口,有默认实现类DefaultLoadControl,里面有个方法shouldContinueLoading,每次缓冲的时候都会调用这个方法判断当前状态是否需要缓冲,那就好办了,我们新建一个类实现LoadContorl接口,模仿DefaultLoadControl实现对应方法,然后在shouldContinueLoading加上自己的控制逻辑,如加一个开关变量来控制是否需缓存。
这段话引用前辈的,原文参考: exoplayer 缓冲控制
看完这段话你是不是顿悟了呢,下面放上源码供参考哟,资历尚浅,代码略显粗鄙,各位将就这看。
1、LoadControl的实现:
public final class JCBufferingLoadControl implements LoadControl {
public static final int DEFAULT_MIN_BUFFER_MS = 15000;
public static final int DEFAULT_MAX_BUFFER_MS = 30000;
public static final int DEFAULT_BUFFER_FOR_PLAYBACK_MS = 2500;
public static final int DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS = 5000;
private static final int ABOVE_HIGH_WATERMARK = 0;
private static final int BETWEEN_WATERMARKS = 1;
private static final int BELOW_LOW_WATERMARK = 2;
private final DefaultAllocator allocator;
private final long minBufferUs;
private final long maxBufferUs;
private final long bufferForPlaybackUs;
private final long bufferForPlaybackAfterRebufferUs;
private final PriorityTaskManager priorityTaskManager;
private int targetBufferSize;
private boolean isBuffering;
public static boolean needBuffering = true;
public JCBufferingLoadControl() {
this(new DefaultAllocator(true,C.DEFAULT_BUFFER_SEGMENT_SIZE));
}
public JCBufferingLoadControl(DefaultAllocator allocator) {
this(allocator, DEFAULT_MIN_BUFFER_MS, DEFAULT_MAX_BUFFER_MS,
DEFAULT_BUFFER_FOR_PLAYBACK_MS,
DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS);
}
public JCBufferingLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBufferMs,long bufferForPlaybackMs, long bufferForPlaybackAfterRebufferMs) {
this(allocator, minBufferMs, maxBufferMs, bufferForPlaybackMs,
bufferForPlaybackAfterRebufferMs, null);
}
public JCBufferingLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBufferMs,long bufferForPlaybackMs, long bufferForPlaybackAfterRebufferMs,PriorityTaskManager priorityTaskManager) {
this.allocator = allocator;
minBufferUs = minBufferMs * 1000L;
maxBufferUs = maxBufferMs * 1000L;
bufferForPlaybackUs = bufferForPlaybackMs * 1000L;
bufferForPlaybackAfterRebufferUs = bufferForPlaybackAfterRebufferMs * 1000L;
this.priorityTaskManager = priorityTaskManager;
}
@Override
public void onPrepared() {
reset(false);
}
@Override
public void onTracksSelected(Renderer[] renderers, TrackGroupArray trackGroups,TrackSelectionArray trackSelections) {
targetBufferSize = 0;
for (int i = 0; i < renderers.length; i++) {
if (trackSelections.get(i) != null) {
targetBufferSize += Util.getDefaultBufferSize(renderers[i].getTrackType());
}
}
allocator.setTargetBufferSize(targetBufferSize);
}
@Override
public void onStopped() {
reset(true);
}
@Override
public void onReleased() {
reset(true);
}
@Override
public Allocator getAllocator() {
return allocator;
}
@Override
public boolean shouldStartPlayback(long bufferedDurationUs, boolean rebuffering) {
long minBufferDurationUs = rebuffering ? bufferForPlaybackAfterRebufferUs :bufferForPlaybackUs;
return minBufferDurationUs <= 0 || bufferedDurationUs >= minBufferDurationUs;
}
@Override
public boolean shouldContinueLoading(long bufferedDurationUs) {
int bufferTimeState = getBufferTimeState(bufferedDurationUs);
boolean targetBufferSizeReached = allocator.getTotalBytesAllocated() >= targetBufferSize;
boolean wasBuffering = isBuffering;
isBuffering = bufferTimeState == BELOW_LOW_WATERMARK
|| (bufferTimeState == BETWEEN_WATERMARKS && isBuffering &&!targetBufferSizeReached);
if (priorityTaskManager != null && isBuffering != wasBuffering) {
if (isBuffering) {
priorityTaskManager.add(C.PRIORITY_PLAYBACK);
} else {
priorityTaskManager.remove(C.PRIORITY_PLAYBACK);
}
}
Log.d("JCBufferingLoadControl", "isBuffering : " + isBuffering + "; needBuffering : " + needBuffering);
return isBuffering && needBuffering;
}
private int getBufferTimeState(long bufferedDurationUs) {
return bufferedDurationUs > maxBufferUs ? ABOVE_HIGH_WATERMARK : (bufferedDurationUs < minBufferUs ? BELOW_LOW_WATERMARK : BETWEEN_WATERMARKS);
}
private void reset(boolean resetAllocator) {
targetBufferSize = 0;
if (priorityTaskManager != null && isBuffering) {
priorityTaskManager.remove(C.PRIORITY_PLAYBACK);
}
isBuffering = false;
if (resetAllocator) {
allocator.reset();
}
}
2、LoadControl的使用:
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(mBandwidthMeter);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
LoadControl loadControl = new JCBufferingLoadControl();
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(((FuckBean) msg.obj).context, trackSelector, loadControl);
simpleExoPlayer.setPlayWhenReady(true);
MediaSource mediaSource = buildMediaSource(((FuckBean) msg.obj).context,Uri.parse(((FuckBean) msg.obj).url));
if (currentPlayingLoop) {
mediaSource = new LoopingMediaSource(mediaSource);
}
simpleExoPlayer.addListener(JCMediaManager.this); simpleExoPlayer.setVideoListener(JCMediaManager.this);
simpleExoPlayer.prepare(mediaSource, true, true);
simpleExoPlayer.setVideoSurface(new Surface(savedSurfaceTexture));
GitHub 加速计划 / ex / ExoPlayer
0
1
下载
最近提交(Master分支:4 个月前 )
dd430f70 - 4 个月前
c00f90aa
(cherry picked from commit 5b2b7f4e6147678dc56ce28826682ba21c4c9508)
6 个月前
更多推荐
已为社区贡献9条内容
所有评论(0)