java.lang.IllegalStateException:android.media.MediaPlayer.stop [英] java.lang.IllegalStateException: android.media.MediaPlayer.stop
问题描述
我注意到我的很多应用程序用户都通过Crashlytics受到了此异常的影响:
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)
这是我所谓的 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;
}
}
该异常会影响具有不同Android版本(9、8、7、6)的不同品牌(小米,三星,摩托罗拉等)的不同设备,但我无法自己复制它.知道为什么会发生这种情况吗?
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?
推荐答案
在检查了mMediaPlayer的空引用之后,检查其状态是否正在使用 .isPlaying()
播放,然后调用 .stop()
.还要在调用 .release()
之前先调用 .reset()
,然后将mMediaPlayer引用设置为null.订单是这样的:
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;
}
这篇关于java.lang.IllegalStateException:android.media.MediaPlayer.stop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!