How to play videos in Android TextureView using Kotlin?

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.



   

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



   
      
         
            
            
         
      
   

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.

Updated on: 2020-04-21T09:44:38+05:30

625 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements