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

Diagram of 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

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



filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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()
        }
  
        // display a toast message if any
        // error occurs while playing the video
        simpleVideoView!!.setOnErrorListener { mp, what, extra ->
            Toast.makeText(applicationContext, "An Error Occured " +
                    "While Playing Video !!!", Toast.LENGTH_LONG).show()
            false
        }
    }
}

chevron_right


Modify strings.xml file

All the strings which are used in the activity are listed in this file.

filter_none

edit
close

play_arrow

link
brightness_4
code

<resources>
    <string name="app_name">VideoView in Kotlin</string>
    <string name="heading_of_activity">Running a video file in an activity</string>
</resources>

chevron_right


Open AndroidManifest.xml file

Below is the code for AndroidManifest.xml file

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>

chevron_right


Run as emulator




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.