如何使用 Kotlin 在 Android TextureView 中播放影片?


本示例演示瞭如何使用 Kotlin 在 Android TextureView 中播放影片。

步驟 1 − 在 Android Studio 中建立新專案,轉到檔案 ⇒ 新專案,並填寫所有必需的詳細資訊以建立新專案。

步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:padding="8dp"
   tools:context=".MainActivity">
   <TextureView
      android:id="@+id/textureView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
</LinearLayout>

步驟 3 − 建立 asset 資料夾並將影片複製貼上到 asset 資料夾中。

步驟 4 − 將以下程式碼新增到 MainActivity.kt

import android.content.res.AssetFileDescriptor
import android.graphics.SurfaceTexture
import android.media.MediaPlayer
import android.media.MediaPlayer.OnVideoSizeChangedListener
import android.os.Build
import android.os.Bundle
import android.view.Surface
import android.view.TextureView
import android.view.TextureView.SurfaceTextureListener
import androidx.appcompat.app.AppCompatActivity
import java.io.IOException
class MainActivity : AppCompatActivity(), SurfaceTextureListener, OnVideoSizeChangedListener {
   private lateinit var textureView: TextureView
   private lateinit var mediaPlayer: MediaPlayer
   private lateinit var fileDescriptor: AssetFileDescriptor
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      textureView = findViewById(R.id.textureView)
      textureView.surfaceTextureListener = this
      mediaPlayer = MediaPlayer()
      try {
         fileDescriptor = assets.openFd("videoplay.mp4")
      }
      catch (e: IOException) {
         e.printStackTrace()
      }
   }
   override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture, width: Int, height: Int) {
      val surface = Surface(surfaceTexture)
      try {
         mediaPlayer.setSurface(surface)
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mediaPlayer.setDataSource(fileDescriptor)
            mediaPlayer.prepareAsync()
            mediaPlayer.setOnPreparedListener { mediaPlayer.start() }
         }
      }
      catch (e: IOException) {
         e.printStackTrace()
      }
   }
   override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {}
   override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
      return false
   }
   override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {}
   override fun onVideoSizeChanged(mp: MediaPlayer, width: Int, height: Int) {}
}

步驟 5 − 將以下程式碼新增到 androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="app.com.kotlipapp">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

讓我們嘗試執行你的應用程式。我假設你已將實際的 Android 移動裝置連線到電腦。要從 Android Studio 執行應用,請開啟專案的活動檔案之一,然後點選工具欄中的執行  圖示。選擇你的移動裝置作為選項,然後檢查顯示預設螢幕的移動裝置 −

點選 此處 下載專案程式碼。

更新於:21-4-2020

464 瀏覽次數

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告