在MediaPlayer上调用stop()而不引起IllegalStateException的正确方法是什么?

时间:2019-08-15 08:44:54

标签: java android android-mediaplayer illegalstateexception

我在停止MediaPlayer时有一段代码,像这样:

@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;
    }
}

但是我在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)

因为如此,我对SO建议(herehere)进行了如下更改:

if (mMediaPlayer.isPlaying()) {
    mMediaPlayer.reset();
    mMediaPlayer.prepare();
    mMediaPlayer.stop();
    mMediaPlayer.release();
    mMediaPlayer = null;
}

但是现在我看到这导致了其他IllegalStateExceptions:

Non-fatal Exception: java.lang.IllegalStateException at android.media.MediaPlayer._reset(MediaPlayer.java)
Non-fatal Exception: java.lang.IllegalStateException at android.media.MediaPlayer._prepare(MediaPlayer.java)
Non-fatal Exception: java.lang.IllegalStateException at android.media.MediaPlayer.isPlaying(MediaPlayer.java)

我不确定在不引起任何这些异常的情况下停止MediaPlayer的正确方法是什么。我还尝试阅读this,但让我比以前更加困惑。有想法吗?

0 个答案:

没有答案