VideoView in Kotlin
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.
The class hierarchy of VideoView class in Kotlin
XML attributes of VideoView widget
XML attribute | Description |
---|---|
android:id | Use to uniquely identify a VideoView |
android:layout_width | To set width of the VideoView |
android:layout_height | To set height of the VideoView |
android:layout_margin | To fix the margin from top, bottom, start and end |
app:layout_constraint | To fix the position in an activity |
Example:
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
Create new project
- 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 > |
Step 1: Open MainActivity.kt file
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.
Kotlin
package com.example.videoviewinkotlin import android.net.Uri import android.os.Bundle import android.view.View import android.widget.MediaController import android.widget.Toast import android.widget.VideoView import 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() true } // 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 } } } |
Java
import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { // references private VideoView simpleVideoView; private MediaController mediaControls; @Override protected void onCreate( @Nullable Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // assigning id of VideoView from // activity_main.xml layout file simpleVideoView = findViewById(R.id.simpleVideoView); if (mediaControls == null ) { // creating an object of media controller class mediaControls = new MediaController( this ); // set the anchor view for the video view mediaControls.setAnchorView(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( new MediaPlayer.OnCompletionListener() { @Override public void onCompletion( MediaPlayer mediaPlayer) { Toast .makeText(getApplicationContext(), "Video Completed" , Toast.LENGTH_LONG) .show(); } }); // display a toast message if any // error occurs while playing the video // and onError Method should return false simpleVideoView.setOnErrorListener( new MediaPlayer.OnErrorListener() { @Override public boolean onError( MediaPlayer mediaPlayer, int i, int i1) { Toast .makeText( getApplicationContext(), "An Error Occurred \" +\n" + " \"While Playing Video !!!" , Toast.LENGTH_SHORT) .show(); return false ; } }); } } |
Step 2: Modify strings.xml file
All the strings which are used in the activity are listed in this file.
Kotlin
<resources> <string name= "app_name" >VideoView in Kotlin/Java</string> <string name= "heading_of_activity" >Running a video file in an activity</string> </resources> |
Step 3: Open AndroidManifest.xml file
Below is the code for AndroidManifest.xml file
XML
<? 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 > |
Step 4: Run as emulator
Contact Us