I've had a piece of code where I stopped the MediaPlayer like:
@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;
}
}
But I noticed in Crashlytics that that throws an exception:
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)
Because of which I made a change to the code with SO recommendations (here and here) like:
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
mMediaPlayer.prepare();
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
But now I see that this had lead to other 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)
I'm not sure what is the correct way of stopping the MediaPlayer without causing any of these exceptions. I also tried giving this a read but it's left me more confused that I was before. Any idea?