背景
項目中主要用的是七牛云播放器,但是因為七牛云播放器暴露出來的問題越來越多,如
弱網seek會出現anr,還有一些native層的崩潰。于是就決定換個播放器用用。之前封裝過ijkplayer,但需要更改FFmpeg配置文件,以及編譯so文件比較繁瑣。最后選擇了阿里云播放器,以阿里云播放器為主,配合原生的MediaPlayer,封裝一套易擴展,易調試,易使用的播放器
設計思路
- 使用外觀模式給使用者提供簡單便利的接口api,由使用者決定選擇何種播放內核
- 通過建造者模式來構建播放配置
- 給所有播放內核抽象出統一的播放控制接口
- 給使用者提供統一的回調接口,中間做一層默認實現,讓使用者按需使用
- 每種播放內核實現統一的播放控制接口和回調接口
- 每種播放內核用一個包裝一層,用來實現一些邏輯適配統一接口
github地址
https://github.com/xiongliangshan/LsMediaPlayer
集成方式
gradle集成
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.xiongliangshan:LsMediaPlayer:1.0.1'
}
直接library依賴
把整個lsplayer模塊拷貝到你的項目中作為一個libaray,然后
implementation project(':lsplayer')
使用方式
基本使用1(xml)
<com.xls.player.widget.LsVideoSurfaceView
android:id="@+id/player"
android:layout_width="0dp"
android:layout_height="300dp"
app:player_type="media"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="w,4:3"/>
或者使用TextureView渲染
<com.xls.player.widget.LsVideoTextureView
android:id="@+id/player"
android:layout_width="0dp"
android:layout_height="300dp"
app:player_type="media"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="w,4:3"/>
然后代碼中開始播放
player.setDataSource(url)
player.prepareAndStart()
基本使用2(使用自定義渲染視圖)
var player: CommonPlayer? = null
player = PlayerEngine.createPlayer(context)
player.setDisplay(surfaceView)
player.setDataSource(url)
player.prepareAndStart()
播放內核無縫切換
如果是使用xml方式,在LsVideoSurfaceView里有一個自定義屬性player_type
(對應LsVideoTextureView里也是一樣的),它的值是枚舉類型,"ali" 和
"media",直接改這個值就可以實現切換播放內核,其他任何地方都不用改。
如果是代碼動態創建播放器,默認就是使用ali播放器內核,可以傳參選擇
fun createPlayer(context:Context,type:PlayerType = PlayerType.Ali):CommonPlayer{
return when(type){
PlayerType.Ali -> AliPlayerWrapper(context)
PlayerType.Media -> MediaPlayerWrapper()
else ->{
AliPlayerWrapper(context)
}
}
}
渲染器無縫切換
如果使用xml方式,直接把 LsVideoSurfaceView 和 LsVideoTextureView 相互替換即可。
如果是代碼創建的播放器實例,直接更換方法參數
player.setDisplay(surfaceView)
player.setDisplay(textureView)
綁定生命周期
在Activity或者Fragment中使用播放器的時候,可以調用如下api
player?.bindLifecycle(owner)
這樣就不用關心播放器資源釋放的問題了,會在onDestroy的時候自動幫你釋放播放器資源
設置回調監聽
player.callback = object :SimpleLsPlayerCallback(){
override fun onVideoSizeChanged(width: Int, height: Int) {
super.onVideoSizeChanged(width, height)
}
override fun onRenderingStart() {
super.onRenderingStart()
}
override fun onInfo(info: LsInfo?) {
super.onInfo(info)
}
override fun onSnapShot(bm: Bitmap?, with: Int, height: Int) {
super.onSnapShot(bm, with, height)
}
override fun onPrepared() {
super.onPrepared()
}
override fun onCompletion() {
super.onCompletion()
}
override fun onError(info: LsInfo) {
super.onError(info)
}
override fun onBufferingBegin() {
super.onBufferingBegin()
}
override fun onBufferingProgress(percent: Int, kbps: Float) {
super.onBufferingProgress(percent, kbps)
}
override fun onBufferingEnd() {
super.onBufferingEnd()
}
override fun onSeekComplete() {
super.onSeekComplete()
}
override fun onStateChanged(state: PlayerState) {
super.onStateChanged(state)
}
override fun onFetchDurationFinished(duration: Long) {
super.onFetchDurationFinished(duration)
}
override fun onPlayProgress(percent: Int) {
super.onPlayProgress(percent)
}
}
這些方法名字取得比較形象,一般能看出來是什么意思,如果實在看不出來,從代碼點擊調到頂層接口定義的地方,每個方法都有注釋。
設置緩存
player?.lsConfig = LsConfig.Builder()
.setLoop(true)
.setCacheDir(LsCacheConfig(it.cacheDir.absolutePath+File.separator+"video"))
.setNetConfig(LsNetConfig(5000,2))
.build()
目前setCacheDir 和 setNetConfig,這個只有在使用ali播放內核的時候才有效。
日志監控
在整個播放器封裝的關鍵地方都打印了日志,內部采用原生的Log。
object SLog{
var switch = true
var printer:LogCallback? = null
fun d(tag: String?,message: String?){
if(switch){
Log.d(tag,message)
}
printer?.printMessage(LsLevel.D,message)
}
fun i(tag:String?,message: String?){
if(switch){
Log.i(tag,message)
}
printer?.printMessage(LsLevel.I,message)
}
fun e(tag:String?,message: String?){
if(switch){
Log.e(tag,message)
}
printer?.printMessage(LsLevel.E,message)
}
}
一般的信息都用的info級別,打印非常頻范的日志使用的是debug級別,錯誤但不致命的使用warn級別,錯誤并且中斷播放的使用error級別。
這里設了一個開關 swtich 默認是打開的,可以根據項目不同環境的包配置打開后者關閉。
這里還有一個LogCallback,主要是把日志回調拋出來給使用者。可以很方便的做存儲或者網絡上傳到服務器。
快速開始
如果只是簡單的播放一個視頻,可以使用LsVideoSurfaceView 和 LsVideoTextureView 的 fastStart方法,一句代碼實現播放
player?.fastStart(url,object :SimpleLsPlayerCallback(){
})
總結:
本次播放器封裝主要是解決項目中零散的播放音視頻需求,讓使用者更方便的切換播放器內核,容易擴展,使用起來更加方便,容易調試。目前支持阿里云播放器和MediaPlayer。有些功能統一接口可能沒有暴露出來,待后續慢慢完善