本文主要是介绍Exoplayer多视频切换和HLS多码率手动切换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
多视频切换说白点就是上一集下一集功能,在网上找了一下发现几篇都是要改源代码的,这么强大的播放器怎么会没有这个功能呢,就换了关键字找,最后找到别人说可以用ConcatenatingMediaSource实现,然后又找具体代码,最后在GitHub里找到例子AesExoPlayer,后来翻了下Exoplayer的demo代码发现也有怎么用ConcatenatingMediaSource的,就是不太明显castdemo
贴代码吧
初始化播放器
private SimpleExoPlayer player;private Timeline.Window window;private Handler handler;private void initPlayer(){handler = new Handler();DefaultTrackSelector trackSelector = new DefaultTrackSelector();player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,Util.getUserAgent(this, "ExoPlayer"));String[] s = {"xxxxxx.mp4","xxxxxx.mp4"};MediaSource[] videoSources = new MediaSource[s.length];for (int i = 0; i < s.length;i++){Uri u = Uri.parse(s[i]);videoSources[i] = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(u, handler, null);}ConcatenatingMediaSource concatenatedSource =new ConcatenatingMediaSource(videoSources);player.prepare(concatenatedSource);window = new Timeline.Window();}
上一集
private void previous() {Timeline timeline = player.getCurrentTimeline();if (timeline.isEmpty()) {return;}int windowIndex = player.getCurrentWindowIndex();timeline.getWindow(windowIndex, window);int previousWindowIndex = timeline.getPreviousWindowIndex(windowIndex, player.getRepeatMode(),false);if (previousWindowIndex != C.INDEX_UNSET) {player.seekTo(previousWindowIndex, C.TIME_UNSET);} else {Log.d("================","第一集");}}
下一集
private void next() {Timeline timeline = player.getCurrentTimeline();if (timeline.isEmpty()) {return;}int windowIndex = player.getCurrentWindowIndex();int nextWindowIndex = timeline.getNextWindowIndex(windowIndex, player.getRepeatMode(),false);if (nextWindowIndex != C.INDEX_UNSET) {player.seekTo(nextWindowIndex, C.TIME_UNSET);} else{Log.d("================","最后一集");}}
HLS多码率切换
HLS多码率切换就是根据m3u8文件内的BANDWIDTH字段来进行切换,BANDWIDTH=700000的时候通过setMaxVideoBitrate(700000)就能切换码率了
2020/4/2更新
由于我跟新了版本到2.+了而我的代码还是1.+的版本,忘了我升级的时候删了代码setForceHighestSupportedBitrate(true),昨天测试发现不能用,现在改成2.+的,2.+是用setMaxVideoSize根据宽高来切换码率的,修改代码
初始化
private SimpleExoPlayer player;private Handler mainHandler;private DefaultTrackSelector trackSelector;private int[] maxWidth = {1920,1280,640};private int[] maxHeight = {1080,720,360};private void initPlayer(){Uri u = Uri.parse("xxx/playlist.m3u8");trackSelector = new DefaultTrackSelector();DefaultTrackSelector.Parameters parameters = trackSelector.getParameters().buildUpon().setMaxVideoSize(maxWidth[0],maxHeight[0]).build();trackSelector.setParameters(parameters);player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,Util.getUserAgent(this, "ExoPlayer"));MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(u, mainHandler, null);player.prepare(videoSource);}
切换
public void change(int index){DefaultTrackSelector.Parameters parameters = trackSelector.getParameters().buildUpon().setMaxVideoSize(maxWidth[index],maxHeight[index]).build();trackSelector.setParameters(parameters);}
Exoplayer的中文文档太少了,英文找的好累啊
这篇关于Exoplayer多视频切换和HLS多码率手动切换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!