0

I'm running through a slideshow in an infinite loop. Every 'x' amount of time, I switch the video playing. First I stop the video, and then I load the new video. My app is crashing when I set the video path, with an error that there is an illegal state. It only happens once every couple thousand uses, so I cannot replicate it. That is, if I leave the app open for 24 hours on 100 devices, it will happen once per day on one device.

runOnUiThread() {
    if (mVideoView!!.isPlaying) {
        mVideoView!!.stopPlayback()
    }
    mVideoView!!.setVideoPath(filePath)
    mVideoView!!.setOnErrorListener { mp, what, extra ->
            LOG.e("Media player error: $what extra: $extra")
            // Something's wrong, try again
            slideshowHandler.removeCallbacksAndMessages(null)
            slideshowHandler.postDelayed(slideshowRunner, 0)
            true
        }
        if (startTime > 0) {
            mVideoView!!.seekTo(startTime)
        }
        mVideoView!!.start()
    }
}


java.lang.IllegalStateException
    at android.media.MediaPlayer.prepareAsync(Native Method)
    at android.widget.VideoView.openVideo(VideoView.java:356)
    at android.widget.VideoView.setVideoURI(VideoView.java:265)
    at android.widget.VideoView.setVideoURI(VideoView.java:248)
    at android.widget.VideoView.setVideoPath(VideoView.java:239)

Any thoughts on what this could be? What state is invalid? Am I doing things in the wrong order?

I can't find any similar issues on SO. The app is running lollipop 5.1 (has to).

Edit: I'm fixing this right now using a try/catch. Seems like there is some underlying bug in Android that is causing this.

Code Wiget
  • 1,540
  • 1
  • 23
  • 35
  • 1
    There might be a timing issue between the completion of the stop-playback work and your subsequent `setVideoPath()` call. `VideoView` is designed for trivial use cases — you may need to use `MediaPlayer` and your own `Surface` (e.g., `SurfaceView`) so you can get more events from the media playback. Or, switch to ExoPlayer. – CommonsWare Jan 18 '20 at 14:49
  • It seems like the only difference between the two is that the videoview is a combination of media player and surface view: https://stackoverflow.com/questions/52057606/videoview-vs-surfaceview-android – Code Wiget Jan 18 '20 at 15:11
  • Yes, but `VideoView` does not expose its `MediaPlayer`, and more importantly all of its listeners and associated events. – CommonsWare Jan 18 '20 at 15:15
  • When you say get more events, you mean more error notices? – Code Wiget Jan 20 '20 at 15:26
  • It may be you cannot call `setVideoPath()` until the stop-playback work has been completed, and that work might be asynchronous. My hope is that there is an event that will get fired when it is safe for you to reuse the `MediaPlayer` for another video, and that you could listen for that event and postpone your `setVideoPath()` call until that point. – CommonsWare Jan 20 '20 at 16:46

0 Answers0