こちらと同じことを、Kotlin で行いました。
再生する動画の名前は、bigbuckbunny.mp4 です。
基礎 動画再生
実行結果
環境
app/build.gradle.kts
compileSdk = 34
minSdk = 26
targetSdk = 34
データ
raw というフォルダーを作成
Open In でファイルをドラッグ
画面
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">
<Button
android:id="@+id/buttonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="145dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="145dp"
android:layout_marginBottom="16dp"
android:onClick="playMovie"
android:text="動画再生"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.481"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<VideoView
android:id="@+id/videoViewId"
android:layout_width="410dp"
android:layout_height="652dp"
android:layout_marginTop="11dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonId" />
</androidx.constraintlayout.widget.ConstraintLayout>
プログラム
MainActivity.kt
package com.example.movie03
import android.os.Bundle
import android.view.View
import android.widget.MediaController
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun playMovie(view: View) {
val videoView = findViewById<VideoView>(R.id.videoViewId)
// ビデオファイルの取得
videoView.setVideoPath("android.resource://" + packageName + "/" + R.raw.bigbuckbunny)
// ビデオのコントロール
val mediaController = MediaController(this)
mediaController.setAnchorView(videoView)
videoView.setMediaController(mediaController)
// 再生
videoView.start()
}
}