1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Android Studio: 動画の再生

Posted at

こちらと同じことを、Kotlin で行いました。
再生する動画の名前は、bigbuckbunny.mp4 です。
基礎 動画再生

実行結果

image.png

環境

app/build.gradle.kts
    compileSdk = 34
    minSdk = 26
    targetSdk = 34

データ

raw というフォルダーを作成
Open In でファイルをドラッグ

image.png

画面

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()
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?