ExoPlayer实现4G网络下暂停缓存功能

最近接到一个需求,要求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));
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值