I'm trying to play a video after another video finishes playing. Keep in mind that this is TextureView
NOT VideoView
. The first videos shows fine, no problemo. And the setOnCompletionListener
fires just fine, but when it gets to the setDataSource
line, it crashes with this log:
2022-07-28 20:59:09.485 12081-12081/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.videobackgroundexample, PID: 12081
java.lang.IllegalStateException
at android.media.MediaPlayer._setDataSource(Native Method)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1335)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1302)
at com.example.videobackgroundexample.MainActivity.onSurfaceTextureAvailable$lambda-1(MainActivity.kt:71)
at com.example.videobackgroundexample.MainActivity.$r8$lambda$52mJcje_qxHAhyGaOIjeM1Dzm4w(Unknown Source:0)
at com.example.videobackgroundexample.MainActivity$$ExternalSyntheticLambda0.onCompletion(Unknown Source:2)
at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:4263)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8595)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
This is my OnCompletionListener
(I do have the "confetti_floor_video.mp4" file in the assets folder):
mediaPlayer!!.setOnCompletionListener {
val fileDescriptor = assets.openFd("confetti_floor_video.mp4")
mediaPlayer!!.setDataSource(fileDescriptor) <-- line that the crash log is complaining about.
mediaPlayer!!.start()
}
Let me know if I need to provide more code and don't hesitate to ask questions. Thanks!
I finally fixed the issue by using this:
mediaPlayer!!.setOnCompletionListener {
val fileDescriptor = assets.openFd("confetti_floor_video.mp4")
mediaPlayerObj!!.reset()
mediaPlayerObj!!.setDataSource(
fileDescriptor!!.fileDescriptor,
fileDescriptor!!.startOffset,
fileDescriptor!!.length
)
mediaPlayerObj!!.prepareAsync()
}
Hope this helps someone else :)