VideoView in Kotlin

08 Jun, 2022
VideoView class of Kotlin is used to display video files in the android application. This class supports the 3gp and MP4 video formats. VideoView class is capable of playing a video file either from local storage, specific URL or from a resource file. The drawback of this class is that it does not retain the full state of the video file if the application goes into the background it means that the current play position, play state, or any kind of subtitle track can not be restored. 
 
 
 
 
This example demonstrates steps involved in adding a video file in an android activity from local storage. A media controller is also added to the activity to control the play and pause the position of the video. 
Note: Following steps are performed on Android Studio version 4.0
 
Click on File, then New => New Project.Select language as Kotlin.Select the minimum SDK as per your need.
Click on File, then New => New Project.
Select language as Kotlin.
Select the minimum SDK as per your need.
Add VideoView in activity_main.xml file
Below is the code for activity_main.xml file to add a TextView and a VideoView in an activity.  
xml
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#168BC34A"    tools:context=".MainActivity">     <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:fontFamily="@font/roboto"        android:isScrollContainer="false"        android:text="@string/heading_of_activity"        android:textAlignment="center"        android:textColor="#1FBC26"        android:textSize="30sp"        android:textStyle="bold"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintVertical_bias="0.01999998" />     <VideoView        android:id="@+id/simpleVideoView"        android:layout_width="0dp"        android:layout_height="0dp"        android:layout_marginStart="10dp"        android:layout_marginTop="20dp"        android:layout_marginEnd="10dp"        android:layout_marginBottom="10dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/textView" /> </androidx.constraintlayout.widget.ConstraintLayout>
Below is the code for MainActivity.kt file to access VideoView widget in Kotlin file and to add a media controller for it. 
Note: Make sure to create a directory named raw in the resource file of your project and add the video file in that directory using file explorer.
 
Java
package com.example.videoviewinkotlin import android.net.Uriimport android.os.Bundleimport android.view.Viewimport android.widget.MediaControllerimport android.widget.Toastimport android.widget.VideoViewimport androidx.appcompat.app.AppCompatActivity  class MainActivity :  AppCompatActivity() {     // declaring a null variable for VideoView    var simpleVideoView: VideoView? = null     // declaring a null variable for MediaController    var mediaControls: MediaController? = null     override fun onCreate(savedInstanceState: Bundle?){        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)         // assigning id of VideoView from        // activity_main.xml layout file        simpleVideoView = findViewById<View>(R.id.simpleVideoView) as VideoView         if (mediaControls == null) {            // creating an object of media controller class            mediaControls = MediaController(this)             // set the anchor view for the video view            mediaControls!!.setAnchorView(this.simpleVideoView)        }         // set the media controller for video view        simpleVideoView!!.setMediaController(mediaControls)         // set the absolute path of the video file which is going to be played        simpleVideoView!!.setVideoURI(Uri.parse("android.resource://"                                       + packageName + "/" + R.raw.gfgvideo))         simpleVideoView!!.requestFocus()         // starting the video        simpleVideoView!!.start()         // display a toast message        // after the video is completed        simpleVideoView!!.setOnCompletionListener {            Toast.makeText(applicationContext, "Video completed",                Toast.LENGTH_LONG).show()        }         // display a toast message if any        // error occurs while playing the video        simpleVideoView!!.setOnErrorListener { mp, what, extra ->            Toast.makeText(applicationContext, "An Error Occurred " +                    "While Playing Video !!!", Toast.LENGTH_LONG).show()            false        }    }}
All the strings which are used in the activity are listed in this file. 
Java
<resources>    <string name="app_name">VideoView in Kotlin</string>    <string name="heading_of_activity">Running a video file in an activity</string></resources>
Below is the code for AndroidManifest.xml file 
Java
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http:// schemas.android.com/apk/res/android"    package="com.example.videoviewinkotlin">     <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>        <meta-data            android:name="preloaded_fonts"            android:resource="@array/preloaded_fonts" />    </application> </manifest>
surindertarika1234
nikhatkhan11
android
Android-View
Kotlin Android
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Android SDK and it's Components
Flutter - Custom Bottom Navigation Bar
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android UI Layouts
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
How to Communicate Between Fragments in Android?