1. 配置参数
1.1 AndroidManifest.xml 文件添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
1.2 http 明文请求设置
android:usesCleartextTraffic="true"
1.3 activity 配置屏幕变化,不重新加载 Activity
//屏幕方向发生变更 - 用户旋转设备/屏幕布局发生变更/当前屏幕尺寸发生变更
android:name=".MainActivity"
android:configChanges="orientation|screenLayout|screenSize"
1.4 开启协程引用库
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.0-alpha03'
1.5.视频渲染 Surface View 或者 Texture View
2. 布局文件 activity_main.xml
<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">
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" />
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" />
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)
val progressBar = findViewById
(R.id.progressBar) val progressBar2 = findViewById
(R.id.progressBar2) 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 {
progressBar2.visibility = INVISIBLE
progressBar.max = it.duration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
it.playbackParams = PlaybackParams().apply {
if (videoView.isPlaying) {
progressBar.progress = videoView.currentPosition
savePosition = videoView.currentPosition
4. 效果图