问题
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?
回答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;
}
来源:https://stackoverflow.com/questions/57425009/java-lang-illegalstateexception-android-media-mediaplayer-stop