![]() |
VOOZH | about |
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.
kotlin.Any
↳ android.view.View
↳ android.view.SurfaceView
↳ android.widget.VideoView
| 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 |
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.
Below is the code for activity_main.xml file to add a TextView and a VideoView in an activity.
activity_main.xml:
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.