1. 配置参数
1.1 AndroidManifest.xml 文件添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
1.2 http 明文请求设置
android:usesCleartextTraffic="true"
1.3 activity 配置屏幕变化,不重新加载 Activity
//屏幕方向发生变更 - 用户旋转设备/屏幕布局发生变更/当前屏幕尺寸发生变更
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenLayout|screenSize"
android:exported="true">
1.4 开启协程引用库
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.0-alpha03'
1.5.视频渲染 Surface View 或者 Texture View
2. 布局文件 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<VideoView
android:id="@+id/videoView"
android:layout_width="2000dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. 使用 VideoView, MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var videoView: VideoView
private var savePosition: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 26.51
val progressBar = findViewById<ProgressBar>(R.id.progressBar)
val progressBar2 = findViewById<ProgressBar>(R.id.progressBar2)
//val videoPath = "android.resource://$packageName/${R.raw.red}"
val videoPath = "https://media.w3.org/2010/05/sintel/trailer.mp4"
videoView = findViewById(R.id.videoView)
videoView.setVideoPath(videoPath)
//设置导航控制布局
videoView.setMediaController(MediaController(this))
videoView.setOnPreparedListener {
//Log.i("MyTag", "$it")
//it.seekTo(3000)
//duration 视频总的长度
progressBar2.visibility = INVISIBLE
progressBar.max = it.duration
it.isLooping = true//循环播放
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
it.playbackParams = PlaybackParams().apply {
speed = 1f //倍数播放
// pitch = 0.7f //变声,提高音调 尖锐,减少低沉
}
}
it.seekTo(savePosition)
//it.start()
}
lifecycleScope.launch {
while (true) {
if (videoView.isPlaying) {
progressBar.progress = videoView.currentPosition
}
delay(500)
}
}
//videoView.start()
}
override fun onPause() {
super.onPause()
savePosition = videoView.currentPosition
}
}
4. 效果图