How to play videos in Android TextureView using Kotlin?

Kotlin Apps/ApplicationsMobile Development

This example demonstrates how to play videos in Android TextureView using Kotlin.

Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project.

Step 2 − Add the following code to 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>

Step 3 − Create an asset folder and copy-paste the video into the asset folder.

Step 4 − Add the following code to 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) {}
}

Step 5 − Add the following code to 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>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Click here to download the project code.

raja
Published on 21-Apr-2020 13:13:19
Advertisements