播放視頻
使用kotlin
使用MediaController
+VideoView
實(shí)現(xiàn)方式視頻播放和常用控制
這種方式是最簡(jiǎn)單的實(shí)現(xiàn)方式席函,是Android原生框架提供的方法,有比較好的兼容性
VideoView
繼承了SurfaceView同時(shí)實(shí)現(xiàn)了MediaPlayerControl接口冈涧,直接在layout中放置即可
MediaController
則是安卓封裝的輔助控制器茂附,帶有暫停正蛙,播放,停止营曼,進(jìn)度條等控件乒验,在播放前和VideoView關(guān)聯(lián)即可
demo介紹
demo代碼地址 https://git.dev.tencent.com/zhoulei26/coairPlayer.git
demo整體介紹:
一個(gè)MainActivity
負(fù)責(zé)權(quán)限申請(qǐng),layout中放置一個(gè)由navigation負(fù)責(zé)頁面跳轉(zhuǎn)的fragment溶推。
兩個(gè)fragment徊件,VideoFragment
負(fù)責(zé)視頻播放,VideoListFragment
展示視頻列表
關(guān)鍵代碼和解析
下面通過關(guān)鍵點(diǎn)代碼說明如何實(shí)現(xiàn)視頻播放與控制 只展示關(guān)鍵代碼
布局文件 fragment_video.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
... />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="list"
.../>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="單曲循環(huán)"
.../>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btReplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重播"
.../>
</androidx.constraintlayout.widget.ConstraintLayout>
demo中有兩個(gè)全局值蒜危,一個(gè)用于指示視頻索引虱痕,一個(gè)是全部視頻列表。項(xiàng)目中辐赞,可以封裝到viewModle中
/** 當(dāng)前播放視頻的索引 */
var curIndexOfVideos = 0
/** 視頻地址列表 */
val videoPathList = listOf(
PathUtils.getExternalStoragePath() + File.separator + "routon" + File.separator +
"3.avi",
PathUtils.getExternalStoragePath() + File.separator + "routon" + File.separator +
"1.mp4",
PathUtils.getExternalStoragePath() + File.separator + "routon" + File.separator +
"55747818-1-192.mkv"
)
關(guān)聯(lián)VideoView與MediaController部翘,播放視頻
/** 設(shè)置videoView視頻地址 */
fun setVideoPath() {
videoView.setVideoPath(videoPathList[curIndexOfVideos])
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
....
setVideoPath()
//創(chuàng)建MediaController對(duì)象
val mediaController = MediaController(activity)
//VideoView與MediaController建立關(guān)聯(lián)
videoView.setMediaController(mediaController)
//讓VideoView獲取焦點(diǎn)
videoView.requestFocus()
//開始播放
videoView.start()
...
}
播放控制
下一曲\上一曲
/** 播放下一曲 */
fun palyNextView() {
curIndexOfVideos++
if (curIndexOfVideos == videoPathList.size) {
curIndexOfVideos = 0
}
setVideoPath()
videoView.start()
}
/** 播放上一曲 */
fun playPrevVideo() {
curIndexOfVideos--
if (curIndexOfVideos < 0) {
curIndexOfVideos = videoPathList.size - 1
}
setVideoPath()
videoView.start()
}
//注冊(cè)監(jiān)聽
mediaController.setPrevNextListeners({
//點(diǎn)擊下一曲
LogUtils.i("next")
palyNextView()
}, {
//點(diǎn)擊上一曲
LogUtils.i("prev")
playPrevVideo()
})
暫停/播放 由MediaController自帶實(shí)現(xiàn)
循環(huán)模式
//由一個(gè)標(biāo)志位控制
var singleCycle = false
//點(diǎn)擊bt控制該標(biāo)志位
btMode.apply {
text = if (singleCycle) {
"單曲循環(huán)"
} else {
"列表循環(huán)"
}
clickWithTrigger {
singleCycle = !singleCycle
text = if (singleCycle) {
"單曲循環(huán)"
} else {
"列表循環(huán)"
}
}
}
//注冊(cè)播放完成之后監(jiān)聽
videoView.setOnCompletionListener {
if (singleCycle) {
//定位到開始位置,然后開始播放
videoView.seekTo(0)
videoView.start()
} else {
//播放下一曲
palyNextView()
}
}
重播
btReplay.clickWithTrigger {
//調(diào)用videoView自己方法
videoView.resume()
}
列表點(diǎn)選播放
一個(gè)簡(jiǎn)單的列表响委。點(diǎn)擊修改全局變量curIndexOfVideos新思,然后返回視頻界面