Open In App

How to Play Videos on TextureView in Android?

Last Updated : 16 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

TextureView in Android is used to display content streams that can be an instance of a video or an OpenGL scene. The source could be local as well as on the Internet. This view behaves as a regular view without creating a separate window and can only be used in a hardware-accelerated window. So in this article, we will show you how you could play a locally saved video in a TextureView in Android. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Add a video in the assets folder

Create an assets folder and copy-paste any sample video of choice. To know about creating an assets folder follow this article: Assets Folder in Android Studio

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextureView as shown below.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <TextureView
        android:id="@+id/texture_view_1"
        android:layout_width="300sp"
        android:layout_height="200sp"
        android:layout_centerInParent="true"/>
 
</RelativeLayout>


 

 

Step 4: Working with the MainActivity.kt file

 

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

 

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<TextureView>(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) {}
}


 

 

Output:

 

You can see that TextureView displays the video.

 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads