当前位置:   article > 正文

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

exoplayer2 defaultloadcontrol

最近接到一个需求,要求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();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

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));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/299262
推荐阅读
相关标签
  

闽ICP备14008679号