1.效果展示:
点击选择视频按钮后:
点击用相册打开后:
点击视频列表中的某个视频,会返回APP并自动播放所选视频
2.三步实现:
- 跳转到本地视频列表
- 点击想播放的视频,带回所选视频数据
- 播放该视频,可通过视频地址进行其他处理
3.直接上代码:
Activity:
Kotlin
class VideoActivity(override val mContentView: Int = R.layout.activity_video) : BaseActivity() {
private var mVideoListCode = 321
private var mVideoView: VideoView? = null
private var mPlayView: View? = null
override fun initView()
mVideoView = findViewById(R.id.video_view)
mPlayView = findViewById(R.id.play_view)
}
override fun initData() {
LogUtil.e("initData")
//自动开始播放
startVideo()
}
@RequiresApi(Build.VERSION_CODES.R)
override fun initListener() {
//播放器点击
mVideoView?.setOnClickListener {
if (mVideoView?.isPlaying == true) {
mVideoView?.pause()
mPlayView?.visibility = View.VISIBLE
} else {
mVideoView?.start()
mPlayView?.visibility = View.GONE
}
}
//点击跳转视频列表,然后点击视频返回视频地址
findViewById<View>(R.id.video_go_list).setOnClickListener {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, mVideoListCode)
}
}
private var mVideoPath: String? = null
private fun startVideo() {
mVideoView?.reset()
mVideoView?.mediaPlayer = SystemMediaPlayer().apply {
if (mVideoPath != null) {
setDataSource(
this@VideoActivity, Uri.fromFile(File(mVideoPath ?: ""))
)
} else {
setDataSource(
this@VideoActivity,
Uri.parse("https://vod.pipi.cn/fe5b84bcvodcq1251246104/658e4b085285890797861659749/f0.mp4")
)
}
}
mVideoView?.prepare()
mVideoView?.start()
}
@Deprecated("Deprecated in Java") //返回选择的视频
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == mVideoListCode) {
data?.data?.let {
mPlayView?.visibility = View.GONE //隐藏封面
val uri: Uri? = data.data
//数据库查询操作。
val cursor: Cursor? =
uri?.let { it1 -> contentResolver.query(it1, null, null, null, null) };
if (cursor != null) {
if (cursor.moveToFirst()) {
mVideoPath = cursor.getString(// 视频路径:MediaStore.Audio.Media.DATA
cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)
)
}
cursor.close();
}
startVideo()
}
}
super.onActivityResult(requestCode, resultCode, data)
}
}
Layout:
bash
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<org.salient.artplayer.ui.VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="300dp" />
<ImageView
android:id="@+id/play_view"
android:src="@mipmap/play_view"
android:layout_width="match_parent"
android:padding="120dp"
android:visibility="gone"
android:background="@color/black3"
android:layout_gravity="center"
android:layout_height="match_parent" />
</FrameLayout>
<Button
android:id="@+id/video_go_list"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择视频"/>
</LinearLayout>
4.所用资源或疑问解答:
BaseActivity?
详细代码和说明看这里:
VideoView ?视频播放器