Error while removing fragment containing videoView in 4.0.4

418 views Asked by At

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.

3

There are 3 answers

2
Dibzmania On BEST ANSWER

I suggest you take a different approach. Before you remove the fragment. Call this code before you attempt to remove the fragment.

videoView.stopPlayback();
videoView.suspend();

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

1
just On

I think you call reset() after release() and this throw illegalstateexception.

1
vidit bhatia On

See the code given on this URL. This is from the actual android github repo. https://github.com/android/platform_frameworks_base/blob/866658261f4613e17ed6f39a74975ad0c9f40767/media/jni/android_media_MediaPlayer.cpp

the reset function from java calls the native _reset function which is given in the CPP file in this URL this is very hard to debug as it is a JNI calls which can not be debugged. So In my opinion it is very hard to know what is going wrong unless we really know what exactly you are doing

Related Questions in ILLEGALSTATEEXCEPTION