📜  如何在 Android 中的 TextureView 上播放视频?

📅  最后修改于: 2022-05-13 01:55:05.848000             🧑  作者: Mango

如何在 Android 中的 TextureView 上播放视频?

Android 中的TextureView用于显示可以是视频实例或 OpenGL 场景的内容流。来源可以是本地的,也可以是 Internet 上的。此视图的行为与常规视图一样,无需创建单独的窗口,并且只能在硬件加速窗口中使用。因此,在本文中,我们将向您展示如何在 Android 的 TextureView 中播放本地保存的视频。 IDE 准备就绪后,请按照以下步骤操作。

分步实施

第 1 步:在 Android Studio 中创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。我们在Kotlin中演示了该应用程序,因此请确保在创建新项目时选择 Kotlin 作为主要语言。

第 2 步:在资产文件夹中添加视频

创建一个资产文件夹并复制粘贴任何选择的示例视频。要了解如何创建资产文件夹,请阅读本文:Android Studio 中的资产文件夹

第 3 步:使用 activity_main.xml 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件。下面是activity_main.xml文件的代码。添加一个TextureView,如下所示。

XML


 
    
 


Kotlin
package org.geeksforgeeks.textureviewexample
 
import android.content.res.AssetFileDescriptor
import android.graphics.SurfaceTexture
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Surface
import android.view.TextureView
 
class MainActivity : AppCompatActivity(), TextureView.SurfaceTextureListener {
 
    // Declaring mediaPlayer and
    // asset file descriptor
    private var mediaPlayer: MediaPlayer? = null
    private lateinit var assetFileDescriptor: AssetFileDescriptor
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // Declaring and initializing
        // TextureView from layout
        val mTextureView = findViewById(R.id.texture_view_1)
        mTextureView.surfaceTextureListener = this@MainActivity
    }
 
    // When app is destroyed,
    // media player stops
    override fun onDestroy() {
        super.onDestroy()
        if (mediaPlayer != null) {
            mediaPlayer?.stop()
            mediaPlayer?.release()
            mediaPlayer = null
        }
    }
 
    // Locate the video file and
    // add to the media player
    override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture, width: Int, height: Int) {
        try {
            assetFileDescriptor = assets.openFd("sample_video.mp4")
        } catch (e: Exception) {
            e.printStackTrace()
        }
 
        try {
            val surface = Surface(surfaceTexture)
            mediaPlayer = MediaPlayer()
            mediaPlayer?.setDataSource(
                assetFileDescriptor.fileDescriptor,
                assetFileDescriptor.startOffset,
                assetFileDescriptor.length
            )
            mediaPlayer?.prepareAsync()
            mediaPlayer?.setSurface(surface)
            mediaPlayer?.isLooping = true
            mediaPlayer?.setOnPreparedListener { player -> player?.start() }
            mediaPlayer?.setOnErrorListener { _, _, _ -> false }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
 
    override fun onSurfaceTextureSizeChanged(p0: SurfaceTexture, p1: Int, p2: Int) {}
 
    override fun onSurfaceTextureDestroyed(p0: SurfaceTexture): Boolean {
        return false
    }
 
    override fun onSurfaceTextureUpdated(p0: SurfaceTexture) {}
}


第 4 步:使用 MainActivity.kt 文件

转到MainActivity.kt文件并参考以下代码。下面是MainActivity.kt文件的代码。代码中添加了注释以更详细地理解代码。

科特林

package org.geeksforgeeks.textureviewexample
 
import android.content.res.AssetFileDescriptor
import android.graphics.SurfaceTexture
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Surface
import android.view.TextureView
 
class MainActivity : AppCompatActivity(), TextureView.SurfaceTextureListener {
 
    // Declaring mediaPlayer and
    // asset file descriptor
    private var mediaPlayer: MediaPlayer? = null
    private lateinit var assetFileDescriptor: AssetFileDescriptor
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // Declaring and initializing
        // TextureView from layout
        val mTextureView = findViewById(R.id.texture_view_1)
        mTextureView.surfaceTextureListener = this@MainActivity
    }
 
    // When app is destroyed,
    // media player stops
    override fun onDestroy() {
        super.onDestroy()
        if (mediaPlayer != null) {
            mediaPlayer?.stop()
            mediaPlayer?.release()
            mediaPlayer = null
        }
    }
 
    // Locate the video file and
    // add to the media player
    override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture, width: Int, height: Int) {
        try {
            assetFileDescriptor = assets.openFd("sample_video.mp4")
        } catch (e: Exception) {
            e.printStackTrace()
        }
 
        try {
            val surface = Surface(surfaceTexture)
            mediaPlayer = MediaPlayer()
            mediaPlayer?.setDataSource(
                assetFileDescriptor.fileDescriptor,
                assetFileDescriptor.startOffset,
                assetFileDescriptor.length
            )
            mediaPlayer?.prepareAsync()
            mediaPlayer?.setSurface(surface)
            mediaPlayer?.isLooping = true
            mediaPlayer?.setOnPreparedListener { player -> player?.start() }
            mediaPlayer?.setOnErrorListener { _, _, _ -> false }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
 
    override fun onSurfaceTextureSizeChanged(p0: SurfaceTexture, p1: Int, p2: Int) {}
 
    override fun onSurfaceTextureDestroyed(p0: SurfaceTexture): Boolean {
        return false
    }
 
    override fun onSurfaceTextureUpdated(p0: SurfaceTexture) {}
}

输出:

您可以看到 TextureView 显示视频。