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, a 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.
Class Hierarchy:
kotlin.Any
âģ android.view.View
âģ android.view.SurfaceView
âģ android.widget.VideoView
Some Important XML attributes of the 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 of VideoView in Kotlin
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.
Step 1: Create new project
- Click on File, then New => New Project.
- Select language as Kotlin.
- Select the minimum SDK as per your need.
Step 2: 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.
activity_main.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">
<VideoView
android:id="@+id/simpleVideoView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: 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.
MainActivity.kt:
package org.geeksforgeeks.demo
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 lateinit variable for VideoView
lateinit var simpleVideoView: VideoView
// declaring a lateinit variable for MediaController
lateinit var mediaControls: MediaController
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.isInitialized) {
// 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.sample_video))
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
}
}
}