如何使用 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 浏览次数

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.