Android Studio (Kotlin) – VideoView Goes Black After Switching Apps

Issue

I have a very simple video playing in the bottom third of my main activity, on a loop. When the app is minimized and then I come back to it, the video will have stopped playing, displaying a black screen instead, and there seems to be no way to get it playing again. (I do not want video controls.)

Here’s my MainActivity.kt:

import android.app.ActionBar
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity

import android.widget.TextView
import android.graphics.drawable.ColorDrawable

class MainActivity :  AppCompatActivity() {

    // declaring a null variable for VideoView
    var simpleVideoView: VideoView?  null

    override fun onCreate(savedInstanceState: Bundle?){
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        supportActionBar?.setDisplayShowTitleEnabled(false)
        supportActionBar?.hide()

        // assigning id of VideoView from
        // activity_main.xml layout file
        simpleVideoView  findViewById<View>(R.id.videoView) as VideoView

        // set the absolute path of the video file which is going to be played
        simpleVideoView!!.setVideoURI(Uri.parse("android.resource://"
                + packageName + "/" + R.raw.gp))

        simpleVideoView!!.requestFocus()

        // starting the video
        simpleVideoView!!.start()

        simpleVideoView!!.setOnCompletionListener { simpleVideoView!!.start() }

    }

}

Solution

The simplest solution would be:

class MainActivity : AppCompatActivity() {

    var simpleVideoView: VideoView?  null
    var currentPosition  0

    override fun onCreate(savedInstanceState: Bundle?){
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        supportActionBar?.setDisplayShowTitleEnabled(false)
        supportActionBar?.hide()


        simpleVideoView  findViewById<View>(R.id.videoView) as VideoView


        simpleVideoView!!.setVideoURI(Uri.parse("android.resource://"
            + packageName + "/" + R.raw.gp))

        simpleVideoView!!.setOnCompletionListener {     simpleVideoView!!.start() }

    if (savedInstanceState ! null) {
        var position  savedInstanceState.getInt("position")
        simpleVideoView!!.seekTo(position)
    }

}

    override fun onStart() {
        super.onStart()
        if (currentPosition ! 0)
             simpleVideoView!!.seekTo(currentPosition)

        simpleVideoView!!.start()

    }

    override fun onResume() {
        super.onResume()
        if (!simpleVideoView!!.isPlaying) {
            if (currentPosition ! 0)
                simpleVideoView!!.seekTo(currentPosition)

            simpleVideoView!!.start()
        }
    }

    override fun onPause() {
        super.onPause()
        simpleVideoView!!.pause()
        currentPosition  simpleVideoView!!.currentPosition
    }

    override fun onStop() {
         simpleVideoView!!.pause()
         super.onStop()
     }

    override fun onSaveInstanceState(outState: Bundle) {
        outState.putInt("position", currentPosition)
        super.onSaveInstanceState(outState)
    }

}

This restarts the video at the same point where it left when rotating the device. You might want to check out the exoplayer from android instead of the VideoView.

Answered By – Cristi

Leave a Comment