surfaceCreated:
@Override public void surfaceCreated(SurfaceHolder holder) { mIsSurfaceCreated = true; mCurrentPlayer.setdisplay(holder); }
surfaceDestroy:
@Override public void surfaceDestroyed(SurfaceHolder holder) { mIsSurfaceCreated = false; // Could be called after player was released in onDestroy. if (mCurrentPlayer != null) { mCurrentPlayer.setdisplay(null); } }
堆栈跟踪:
java.lang.IllegalArgumentException: The surface has been released at android.media.MediaPlayer._setVideoSurface(Native Method) at android.media.MediaPlayer.setdisplay(MediaPlayer.java:660) at com.xxx.xxx.view.VideoPlayerView.surfaceCreated(VideoPlayerView.java:464) at android.view.SurfaceView.updateWindow(SurfaceView.java:543) at android.view.SurfaceView.access$000(SurfaceView.java:81) at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169) at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1644) at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:4945) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method)
有什么想法可能会出错? SurfaceHolder可能会破坏背景线程上的表面,然后等待主线程(当前由surfaceCreated占用)完成它的块,然后才能在主线程上调用surfaceDestroyed(我甚至不认为锁可以修复)?还有什么?
更新 – 再次向下钻一点,我发现什么原因“表面被释放”为thrown:
哪个引用android_view_Surface_getSurface可以找到here:
这是我缺乏C知识的地方,它看起来像是试图锁定在表面上,如果返回的表面不能为null.一旦返回为null,将抛出IllegalArgumentException.
解决方法
我的调查显示,SurfaceView中有一个错误,导致将无效表面传递给surfaceCreated回调方法.
这是在android repo修复它的提交:link.
似乎在4.2版本中引入了Android源代码.而且,从应用程序崩溃看,无效表面引起的崩溃只发生在4.0和4.1.
所以,我可以假设在4.0之前将无效表面传递给MediaPlayer是有效的.而在4.0版本中,SurfaceView / MediaPlayer的逻辑也发生了变化,这使得它不再有效.但是,SurfaceView中的代码在4.2之前没有被更新(在SurfaceView中的这个问题是固定的).
我已经检查了android的git repo,真的,版本标记的android-4.0.1_r1不包括修复和版本标记的android-4.2.1_r1包括它.
所以,为了修复它不包含修复的平台,手动检查表面是否有效,然后再设置到MediaPlayer只需要平台4.0和之后:
@Override public void surfaceCreated(final SurfaceHolder holder) { final Surface surface = holder.getSurface(); if ( surface == null ) return; // on pre Ice Scream Sandwich (4.0) versions invalid surfaces seems to be accepted (or at least do not cause crash) final boolean invalidSurfaceAccepted = Build.VERSION.SDK_INT < Build.ICE_CREAM_SANDWICH; final boolean invalidSurface = ! surface.isValid(); if ( invalidSurface && ( ! invalidSurfaceAccepted ) ) return; _mediaPlayer.setdisplay(holder); }
这样,在旧版平台上,无效的表面将成功设置为媒体播放器,视频将播放,在平台4.0-4.1它将抛出无效的表面(我认为surfaceCreated将被再次调用有效的表面)和4.2后来的surfaceCreated将不会被称为无效表面.