I have a fragment that contains a VideoView
and a playlist of videos. It allows the user to click the videos in the playlist to swap the content of the VideoView
. It also allows the user seek around each of the videos. It is not feasible to post the entire code here, but I am trying to narrow the issue down so that I can add a sample.
I am finding that if I rapidly change the source of the VideoView
and seek in the VideoView
, I am getting an IllegalStateException
:
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x415fe8b0)
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: java.lang.IllegalStateException
E/AndroidRuntime: at android.media.MediaPlayer.getVideoWidth(Native Method)
E/AndroidRuntime: at android.widget.VideoView$2.onPrepared(VideoView.java:346)
E/AndroidRuntime: at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2048)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:213)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5225)
E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
As you can see there is no reference to any of my code. If I perform the same actions slowly, I don't seem to have the same issue. From reading questions like:
IllegalStateException calling MediaPlayer.reset()
http://developer.android.com/reference/android/media/MediaPlayer.html#Valid_and_Invalid_States
I assume I am performing an action when the player is in an invalid state, but I don't know what that action is. I have a MediaPlayer.OnErrorListener
set on the VideoView
, but I don't see any errors in the logs. I don't call reset
or release
on the VideoView
at any point.
I initialise the player with:
localPlayerVideoView = (VideoView) view.findViewById(R.id.player)
MediaController mediaController = new MediaController(getActivity());
mediaController.setVisibility(View.GONE);
mediaController.setAnchorView(localPlayerVideoView);
localPlayerVideoView.setMediaController(mediaController);
localPlayerVideoView.setVideoPath(file.getAbsolutePath());
localPlayerVideoView.setOnPreparedListener(localOnPreparedListener);
localPlayerVideoView.setOnErrorListener(localOnErrorListener);
localPlayerVideoView.setOnInfoListener(localOnInfoListener);
localPlayerVideoView.requestFocus();
On prepared:
localPlayerVideoView.seekTo(time);
localPlayerVideoView.start();
Swapping videos:
localPlayerVideoView.setOnPreparedListener(null);
localPlayerVideoView.setPlayPauseListener(null);
localPlayerVideoView.suspend();
localPlayerVideoView.setVideoPath(file.getAbsolutePath());
localPlayerVideoView.setOnPreparedListener(localOnPreparedListener);
localPlayerVideoView.setPlayPauseListener(localPlayPauseListener);
localPlayerVideoView.requestFocus();
Seek in video:
localPlayerVideoView.seekTo(time);
localPlayerVideoView.pause();
Just before the crash happens I have logged that these are the VideoView
functions I call:
setOnPreparedListener(null)
setPlayPauseListener(null)
suspend
setVideoPath
setOnPreparedListener
setPlayPauseListener
requestFocus
onPrepared
onPlaying
getCurrentPosition
onPlaying
getCurrentPosition
getCurrentPosition
onInfo: what: 3
canPause
pause
setOnPreparedListener(null)
setPlayPauseListener(null)
suspend
setVideoPath
setOnPreparedListener
setPlayPauseListener
requestFocus
onPrepared
onPlaying
getCurrentPosition
canPause
pause
canPause
pause
setOnPreparedListener(null)
setPlayPauseListener(null)
suspend
setVideoPath
setOnPreparedListener
setPlayPauseListener
requestFocus
How might I track down this error?