1

I notice that quite a number of my app users are affected by this exception via Crashlytics:

Non-fatal Exception: java.lang.IllegalStateException
       at android.media.MediaPlayer._stop(MediaPlayer.java)
       at android.media.MediaPlayer.stop + 1437(MediaPlayer.java:1437)
       at com.allattentionhere.autoplayvideos.AAH_CustomVideoView.onSurfaceTextureDestroyed + 256(AAH_CustomVideoView.java:256)
       at android.view.TextureView.releaseSurfaceTexture + 249(TextureView.java:249)
       at android.view.TextureView.onDetachedFromWindowInternal + 222(TextureView.java:222)
       at android.view.View.dispatchDetachedFromWindow + 17586(View.java:17586)
       at android.view.ViewGroup.dispatchDetachedFromWindow + 3756(ViewGroup.java:3756)
       at android.view.ViewGroup.dispatchDetachedFromWindow + 3756(ViewGroup.java:3756)
       at android.view.ViewGroup.dispatchDetachedFromWindow + 3756(ViewGroup.java:3756)
       at android.view.ViewGroup.removeViewInternal + 5320(ViewGroup.java:5320)
       at android.view.ViewGroup.removeViewAt + 5267(ViewGroup.java:5267)
       at androidx.recyclerview.widget.RecyclerView$5.removeViewAt + 877(RecyclerView.java:877)
       at androidx.recyclerview.widget.ChildHelper.removeViewAt + 168(ChildHelper.java:168)
       at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeViewAt + 8374(RecyclerView.java:8374)
       at androidx.recyclerview.widget.RecyclerView$LayoutManager.removeAndRecycleViewAt + 8647(RecyclerView.java:8647)
       at androidx.recyclerview.widget.LinearLayoutManager.recycleChildren + 1369(LinearLayoutManager.java:1369)
       at androidx.recyclerview.widget.LinearLayoutManager.recycleViewsFromStart + 1415(LinearLayoutManager.java:1415)
       at androidx.recyclerview.widget.LinearLayoutManager.recycleByLayoutState + 1484(LinearLayoutManager.java:1484)
       at androidx.recyclerview.widget.LinearLayoutManager.fill + 1508(LinearLayoutManager.java:1508)
       at androidx.recyclerview.widget.LinearLayoutManager.scrollBy + 1331(LinearLayoutManager.java:1331)
       at androidx.recyclerview.widget.LinearLayoutManager.scrollVerticallyBy + 1075(LinearLayoutManager.java:1075)
       at androidx.recyclerview.widget.RecyclerView.scrollStep + 1832(RecyclerView.java:1832)
       at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run + 5067(RecyclerView.java:5067)
       at android.view.Choreographer$CallbackRecord.run + 1008(Choreographer.java:1008)
       at android.view.Choreographer.doCallbacks + 804(Choreographer.java:804)
       at android.view.Choreographer.doFrame + 729(Choreographer.java:729)
       at android.view.Choreographer$FrameDisplayEventReceiver.run + 994(Choreographer.java:994)
       at android.os.Handler.handleCallback + 794(Handler.java:794)
       at android.os.Handler.dispatchMessage + 99(Handler.java:99)
       at android.os.Looper.loop + 176(Looper.java:176)
       at android.app.ActivityThread.main + 6662(ActivityThread.java:6662)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 547(RuntimeInit.java:547)
       at com.android.internal.os.ZygoteInit.main + 873(ZygoteInit.java:873)

Here's how I call MediaPlayer.stop:

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        try {
            if (isAndroid5OrGreater()) {
                //pre lollipop needs SurfaceTexture it owns before calling onDetachedFromWindow super
                surface.release();
            }
            if (mMediaPlayer != null) {
                mMediaPlayer.stop();
                mMediaPlayer.release();
                mMediaPlayer = null;
            }

            showThumb.call();
            return true;
        } catch (Exception e) {
            CriticalLogger.error(e);
            return false;
        }
    }

The exception affect different devices from different brands (Xiaomi, Samsung, Motorola etc) with different Android versions (9, 8, 7, 6) but I'm not able to reproduce it myself. Any idea why this could happen?

waseefakhtar
  • 1,373
  • 2
  • 24
  • 47
  • Checking only for null is possibly not sufficient for your setup. There are a couple of states that will cause an Exception when `stop()` is called: https://developer.android.com/reference/android/media/MediaPlayer#Valid_and_Invalid_States. – Mike M. Aug 09 '19 at 07:13
  • @MikeM. I read that `stop()` stops the playback if it's either or started or paused: https://developer.android.com/reference/android/media/MediaPlayer.html#stop() . How many checks do you think it needs before calling `stop()` and in which order? – waseefakhtar Aug 09 '19 at 07:39

1 Answers1

1

after checking for the null reference of mMediaPlayer, check its state is playing with .isPlaying() then call .stop(). also call .reset() before calling .release(), then make the mMediaPlayer reference null. order is like this:

if (mMediaPlayer.isPlaying()) {
    mMediaPlayer.stop();
    mMediaPlayer.reset();
    mMediaPlayer.release();
    mMediaPlayer=null;
}
waseefakhtar
  • 1,373
  • 2
  • 24
  • 47
  • I also read that stop() stops the playback if it's either or started or paused: https://developer.android.com/reference/android/media/MediaPlayer.html#isPlaying() . In addition to checking for `isPlaying()`, Would I need to check if `mMediaPlayer` is paused, as well? – waseefakhtar Aug 09 '19 at 07:44
  • This might be the fix since I just came across a similar answer here: https://stackoverflow.com/a/38839596/2997806 but without the check for `isPlaying` and calling methods in different order, I'm not sure which order to go about. – waseefakhtar Aug 09 '19 at 07:50
  • Calling stop() has no effect on a MediaPlayer object that is already in the Stopped state. –  Aug 09 '19 at 07:54
  • calling stop() may gen. IllegalStateException if it is called in an invalid state. so stop is called on a invalid state of media player. –  Aug 09 '19 at 07:58
  • Seems like `isPlaying` throws an IllegalStateException as well _if the internal player engine has not been initialized, or setDataSource has not been called_. So it's better to just go with https://stackoverflow.com/a/38839596/2997806 – waseefakhtar Aug 14 '19 at 16:23