0
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?

Android端末内の動画を再生する

0
Posted at

Androidで端末内に保存されている動画を再生する方法をメモします。

ライブラリを追加する

今回はMedia3というライブラリを使用します。

  • media3-exoplaye:動画の読み込み・再生処理
  • media3-ui:動画表示や再生コントロール用のUI
dependencies {
    implementation("androidx.media3:media3-exoplayer:1.8.0")
    implementation("androidx.media3:media3-ui:1.8.0")
}

次に、動画を表示するためのPlayerViewと、動画を選択するためのButtonを配置します。
app:use_controller="true" は再生・一時停止・シークバーなどのUIを表示します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/selectButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="動画ボタン" />

    <androidx.media3.ui.PlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        app:use_controller="true" />

</LinearLayout>

実装例

以下に実装例を書きます。

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private var player: ExoPlayer? = null
    private val videoPicker = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
        uri?.let { playVideo(it) }
    }

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

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

        binding.selectButton.setOnClickListener {
            videoPicker.launch("video/*")
        }
    }

    // 動画再生
    private fun playVideo(uri: Uri) {
        player?.release()
        player = ExoPlayer.Builder(this).build()
        binding.playerView.player = player
        val mediaItem = MediaItem.fromUri(uri)
        player?.setMediaItem(mediaItem)
        player?.prepare()
        player?.play()
    }

    override fun onDestroy() {
        super.onDestroy()

        player?.release()
        player = null
    }
}

動画を選択する

端末内の動画を選択するにはActivityResultContracts.GetContent() を使用します。

private val videoPicker = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
    uri?.let { playVideo(it) }
}

videoPicker.launch("video/*") を実行すると、端末のコンテンツ選択画面が表示されます。

選択した動画を再生する

次に、ExoPlayerを使用して動画を再生します。

private var player: ExoPlayer? = null

private fun playVideo(uri: Uri) {
    player?.release()
    player = ExoPlayer.Builder(this).build()
    binding.playerView.player = player

    val mediaItem = MediaItem.fromUri(uri)
    player?.setMediaItem(mediaItem)
    player?.prepare()
    player?.play()
}

メソッドの役割は以下のとおりです。

メソッド 役割
ExoPlayer.Builder() ExoPlayerの生成
playerView.player PlayerViewにプレイヤーを設定
MediaItem.fromUri() 再生対象の動画を指定
setMediaItem() 再生する動画を設定
prepare() 動画の読み込み準備
play() 動画の再生開始
release() プレイヤーのリソース解放

参考

0
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
0
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?