系列文章目录
ExoPlayer架构详解与源码分析(1)——前言
ExoPlayer架构详解与源码分析(2)——Player
ExoPlayer架构详解与源码分析(3)——Timeline
ExoPlayer架构详解与源码分析(4)——整体架构
ExoPlayer架构详解与源码分析(5)——MediaSource
ExoPlayer架构详解与源码分析(6)——MediaPeriod
ExoPlayer架构详解与源码分析(7)——SampleQueue
ExoPlayer架构详解与源码分析(8)——Loader
ExoPlayer架构详解与源码分析(9)——TsExtractor
ExoPlayer架构详解与源码分析(10)——H264Reader
ExoPlayer架构详解与源码分析(11)——DataSource
ExoPlayer架构详解与源码分析(12)——Cache
ExoPlayer架构详解与源码分析(13)——TeeDataSource和CacheDataSource
ExoPlayer架构详解与源码分析(14)——ProgressiveMediaPeriod
ExoPlayer架构详解与源码分析(15)——Renderer
ExoPlayer架构详解与源码分析(16)——LoadControl
ExoPlayer架构详解与源码分析(17)——TrackSelector
前言
根据前篇ExoPlayer架构详解与源码分析(2)——Player,想要直接实现Player接口需要非常复杂的代码逻辑,都写在一个类里肯定不现实,需要通过更多层次的扩展简化来实现,当然ExoPlayer就是这么做的,本篇来讲讲的如何通过BasePlayer来简化设计以及ExoPlayer如何将整个复杂的设计划分给一个个子系统来完成的。
Player的实现
先来看下整体架构
Player接口经过了一层BasePlayer简化,和ExoPlayer扩展。然后由ExoPlayerImpl实现,ExoPlayerImpl内部又依赖ExoPlayerImplInternal,ExoPlayerImplInternal再依据功能划分将任务交由各个组件,主要为MediaSource、Renderer、TrackSelector、LoadControl四大组件。
BasePlayer
先说BasePlayer 是个抽象类,主要作用是简化了Player接口的部分功能。
-
实现了单文件列表增删改等操作,通过将单个MediaItem转为List,交由xxMediaItems实现。
@Override public final void setMediaItem(MediaItem mediaItem) { setMediaItems(ImmutableList.of(mediaItem)); }
-
实例化出Timeline 中的 Window对象,这里主要用于Timeline getWindow 方法时装填的容器,因为Timeline 本身不持有Window或者Period,Timeline获取Window或者Period时都需要传入一个容器去获取,通过调用容器的set方法给容器赋值。
protected BasePlayer() { window = new Timeline.Window(); } @Override public final long getContentDuration() { //获取播放的总时长 Timeline timeline = getCurrentTimeline();//先获取Timeline 由子类实现 return timeline.isEmpty() ? C.TIME_UNSET//将初始化的window对象传入,方法里会将window对象赋值 : timeline.getWindow(getCurrentMediaItemIndex(), window).getDurationMs(); }
-
实现了Player关于播放列表管理的设计,将MediaItem 播放列表查询相关交由Timeline管理,从这里可以看出上面针对MediaItem 的增删改,最终都是会封装到或者同步到Timeline里的,这里后面看到具体实现。