VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/videoview-in-kotlin/

⇱ VideoView in Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

VideoView in Kotlin

Last Updated : 15 Jul, 2025

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 attributeDescription
android:idUse to uniquely identify a VideoView
android:layout_widthTo set width of the VideoView
android:layout_heightTo set height of the VideoView
android:layout_marginTo fix the margin from top, bottom, start and end
app:layout_constraintTo 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

  1. Click on File, then New => New Project.
  2. Select language as Kotlin.
  3. 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:


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:

Step 4: Run as emulator

Comment
Article Tags:

Explore