Click here to Skip to main content
14,768,455 members
Articles » Mobile Development » Android » General
Posted 16 Mar 2020

Tagged as


3 bookmarked

Learn Android programming with Kotlin: Using VideoView and MediaController

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
16 Mar 2020CPOL
How to use VideoView and MediaController in Kotlin
This post will show you how to play a video clip that is either embedded in the app's resources, or available on the internet by using VideoView and MediaController in Kotlin.


Smartphones and tablets are used with many different purposes such as study, entertainment, so on. And watching videos is one of those purposes. So, as Android developers, we must make functions help Android device users to interact with videos easily.

There are many different ways to play a video and control it in an Android app, however, using the VideoView and the MediaController classes can be simplest way.

In this tip, I am going to build a simple app that plays a video clip that is either embedded in the app's resources, or available on the internet by using Kotlin and Android Studio 3.6.1.


VideoView Class

VideoView currently supports the following video formats:

  • H.263
  • H.264 AVC
  • H.265 HEVC
  • MPEG-4 SP
  • VP8
  • VP9

The VideoView class has a wide range of methods that may be called in order to manage the playback of video here. The following methods are used in this article:

  • setVideoUri
  • seekTo
  • start
  • stopPlayback
  • pause
  • setOnPreparedListener
  • setOnCompletionListener
  • getCurrentPosition
  • setMediaController

MediaController Class

The MediaController also provides a set of methods allowing the user to manage the playback of video here. The following method is used in this article:

  • setMediaPlayer

Using the Code

Create the Project and Layout

Create a new Android project and name it MyFirstKotlinApp, choose the Empty activity template, select Kotlin language and leave all other defaults.

In activity_main.xml, delete the existing TextView, and replace it with a VideoView element and two TextView elements with these attributes:

        android:text="Playback completed"

Playing a Video File using VideoView

To play a video file embedded with the app's resources, first, we must import the video file (can download and name it falling_snow.mp4) with the following steps:

  • Right click the res directory in the Project view and select New > Android Resource Directory.

    Image 1

  • Change Resource type to "raw" (Directory name is also changed automatically). Leave all other options as is and click OK.

    Image 2

  • Move (Copy/Cut) the video file (falling_snow.mp4) into (Paste) the raw directory in our project.

    Image 3

To play a video file from the internet, first, in the Android manifest file, add an internet permission to enable the app to access the video file on the internet before the <application> element, at the top level of the manifest:

<manifest xmlns:android=""
    <uses-permission android:name="android.permission.INTERNET" />

Next, in MainActivity.kt, add a constant for the name of the video file in our app’s resource:

private val VIDEO_NAME = "falling_snow"

or a URL for video file from the interner:

private val VIDEO_NAME = 

Also in MainActivity.kt, create a private method called getURI() that tests whether the input string is a URL or a raw resource and return the appropriate URI. This method looks like this:

private fun getURI(videoname:String): Uri{
   if (URLUtil.isValidUrl(videoname)) {
        //  an external URL
        return Uri.parse(videoname);
    } else { //  a raw resource
        return Uri.parse("android.resource://" + getPackageName() +
                "/raw/" + videoname);

We also create a new private method called initPlayer() that calls the getURI() and setVideoUri() methods to set the URI that the VideoView will play and start() on the VideoView to start playing:

private fun initPlayer() {
    var videoUri:Uri = getURI(VIDEO_NAME)

When we stop the video from playing, we must release all the resources held by the VideoView. To do this, we create a private method called releasePlayer(), and call the stopPlayback() method on the VideoView.

private fun releasePlayer(){

Before building and running our app, we need to override Activity lifecycle methods:

  • onStop() to release the media resources when the app is stopped
  • onStart() to initialize the resources when the app is started again
  • onPause() and add a test for versions of Android older than N (lower than 7.0, API 24). If the app is running on an older version of Android, pause the VideoView here.

The onStop() looks like this:

protected override fun onStop() {

The onStart():

protected override fun onStart() {

And the onPause():

protected override fun onPause() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {

So far, we can build and run our app:

Image 4

We cannot control the media playback such as pause, play, fast-forward, or rewind. These functions will be added in the next step.

Control the Video using MediaController Class

To add a MediaController that controls our video, we must implement the following steps:

  • In onCreate() method, create a new MediaController object and use setMediaPlayer() to connect the object to the VideoView:
    val controller = MediaController(this)
  • Use setMediaController() to tell the VideoView that the MediaController will be used to control it:

And now, we can built and run app again. Tapping the VideoView to make the MediaController appear:

Image 5

At the moment, we try to rotate the Android Emulator by clicking the Rotate left button on the Emulator's right bar:

Image 6

The result looks like this:

Image 7

We note that the video restarts from the beginning and here is the behavior that we don’t expect. We can solve this problem by preserving playback position throughout the activity lifecycle with the following steps:

  • In MainActivity.kt, we add a member variable to hold the video's current position (initially 0) and member variable to hold the key for the playback position in the instance state bundle:
    private val PLAYBACK_TIME = "play_time"
    private var currentPos = 0
  • In initPlayer(), after setVideoUri() but before start(), check to see whether the current position is greater than 0, which indicates that the video was playing at some point. Use the seekTo() method to move the playback position to the current position:
    private fun initPlayer() {
        var videoUri:Uri = getURI(VIDEO_NAME)
        if (currentPos > 0) {
        } else {
  • Override the onSaveInstanceState() method to save the value of current time to the instance state bundle. Get the current playback position with the getCurrentPosition() method:
    protected override fun onSaveInstanceState(outState: Bundle?) {
         outState?.putInt(PLAYBACK_TIME, videoview.getCurrentPosition())
  • In onCreate(), check for the existence of the instance state bundle, and update the value of current time with the value from that bundle. Add these lines before you create the MediaController:
    if (savedInstanceState != null) {
        currentPos = savedInstanceState.getInt(PLAYBACK_TIME);

Build and run the app. Try to rotate Emulator again and note how the playback position is saved.

Preparation and Completion Events

The VideoView class defines listeners for several events related to media playback and Listeners for the preparation and completion events are the most common listeners to implement in a media app.

In Java, we must use the onPrepared() and the onCompletion() callbacks for preparation and completion events. In Kotlin, we just implement:

// preparation event
//completion events
videoview.setOnCompletionListener {

In our app, we implement the following steps:

  • In InitPlayer(), we change some things as follows:
    private fun initPlayer() {
        loading.visibility = VideoView.VISIBLE
        completed.visibility = VideoView.INVISIBLE
        var videoUri:Uri = getURI(VIDEO_NAME)
            loading.visibility = VideoView.INVISIBLE
            if (currentPos > 0) {
            } else {
        videoview.setOnCompletionListener {
            completed.visibility = VideoView.VISIBLE

Built and run our app. Each time initPlayer() is called, the loading message should be turned on. It is turned off only when the media is ready to play:

Image 8

When the video finishes playing, the completed message appears:

Image 9

Points of Interest

Using VideoView and MediaController is old title in Android programming and you can easily find a lot of documents about them. However, as a Kotlin beginner, I want to learn how to use these classes in Android programming by using Kotlin from scratch as a way to understand more about Kotlin and how to apply it in Android programming.


  • 15th March, 2020: Initial version
  • 16th March, 2020: Second version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Coding Notes
Software Developer
Vietnam Vietnam
I am a teacher, a software developer. I love reading, travelling and I am also blogging on VietNamese).

Comments and Discussions

QuestionPerfect for what I needed Pin
ScottJenson22-Jun-20 13:31
MemberScottJenson22-Jun-20 13:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.