Android

【Android】VideoView 播放影片 範例

【Android】VideoView 播放影片 範例

VideoView 是 Android 框架中一個用於播放視頻的類。它可以用於在 Android 應用程序中播放本地或遠程視頻。它提供了一系列 API,可以用於控制視頻的播放、暫停、停止等。


文章目錄

  1. VideoView 讀取影片
  2. VideoView 開始、暫停、重播
  3. VideoView 拖曳快轉
  4. VideoView 監聽播放進度

1.VideoView 讀取影片

raw 資料夾
binding.videoView.setVideoURI(Uri.parse("android.resource://packageName/{R.raw.a}"))
網路加載
binding.videoView.setVideoPath("https://firebasestorage.googleapis.com/v0/b/jetpackdemo-b4bda.appspot.com/o/a.mp4?alt=media&token=763afad8-d421-437e-b96c-064f4d95ab2b")

2.VideoView 開始、暫停、重播

activity_main.xml
<?xml version="1.0" encoding="utf-8"?><!-- Use DrawerLayout as root container for activity -->
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <VideoView
            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:keepScreenOn="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:layout_marginTop="16dp"
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="開始"
            app:layout_constraintEnd_toStartOf="@+id/pause"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/videoView" />

        <Button
            android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="暫停"
            app:layout_constraintEnd_toStartOf="@+id/resume"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/start"
            app:layout_constraintTop_toTopOf="@+id/start" />

        <Button
            android:id="@+id/resume"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="重播"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toEndOf="@+id/pause"
            app:layout_constraintTop_toTopOf="@+id/pause" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

MainActivity.kt
class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.start.setOnClickListener {
            binding.videoView.start()
        }

        binding.pause.setOnClickListener {
            binding.videoView.pause()
        }

        binding.resume.setOnClickListener {
            binding.videoView.resume()
        }
    }

}

3.VideoView 拖曳快轉

binding.videoView.setOnTouchListener { _, event ->
    when (event.action) {
        MotionEvent.ACTION_MOVE -> {
            val position = binding.videoView.duration * event.x / binding.videoView.width
            binding.videoView.seekTo(position.toInt())
            binding.time.text = "${(position / binding.videoView.duration * 100).roundToInt()}%"
        }
    }
    true
}

4.VideoView 監聽播放進度

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private val choreographer = Choreographer.getInstance()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.videoView.setOnPreparedListener {
            it.isLooping = true
            it.start()

            choreographer.postFrameCallback(object : Choreographer.FrameCallback {
                override fun doFrame(frameTimeNanos: Long) {
                    updateProgress()
                    choreographer.postFrameCallback(this)
                }
            })
        }
    }

    private fun updateProgress() {
        val position = binding.videoView.currentPosition * 100
        binding.time.text = "${(position / binding.videoView.duration)}%"
    }

}

發表迴響