I have a fragment containing VideoView and few other views.
public class PlayerPane extends Fragment {
... // static variables
private ImageView imageView;
private ImageView gifView;
private VideoView videoView;
private WebView webView;
private PDFView pdfView;
private MyScrollTextView scrollTextView;
private MediaPlayer audioPlayer;
...
@Override
public void onDestroyView() {
if (videoView != null && videoView.isPlaying()) {
LOGGER.info("Stopping videoView");
videoView.stopPlayback();
videoView.suspend();
videoView = null;
}
super.onDestroyView();
}
Whenever I remove this fragment while the video is being played, IllegalStateException is being thrown only in Android 4.0.4 (No issue with versions above 4.0.4).
Following is the exception:
java.lang.IllegalStateException
at android.media.MediaPlayer._reset(Native Method)
at android.media.MediaPlayer.reset(MediaPlayer.java:1236)
at android.widget.VideoView.release(VideoView.java:549)
at android.widget.VideoView.access$2300(VideoView.java:49)
at android.widget.VideoView$6.surfaceDestroyed(VideoView.java:537)
at android.view.SurfaceView.updateWindow(SurfaceView.java:581)
at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:290)
at android.view.View.dispatchDetachedFromWindow(View.java:9823)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2266)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3588)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3568)
at android.view.ViewGroup.removeView(ViewGroup.java:3516)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:951)
at android.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1123)
at android.app.BackStackRecord.run(BackStackRecord.java:592)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
This is how I remove the fragment:
getFragmentManager.beginTransaction().remove(fragmentToBeRemoved).commit();
What is causing this IllegalStateException and how do I resolve this? Anything pointing towards the solution would be of great help.
I suggest you take a different approach. Before you remove the fragment. Call this code before you attempt to remove the fragment.
As is evident from the stack trace, when you remove the fragment, What Android does is to remove the VideoView (it's a view after all) which results in Videoview Attempting to release the underlying Mediaplayer. But the Mediaplayer itself might not be a state to be released which is causing this issue. Ideally this well might have been handled within VideocView, but looks like no