我正在呼叫:
mSurface.lockCanvas(null);
Null是一个可接受的参数,用于提供lockCanvas,表示整个屏幕需要更新。
我正在使用的表面被传递给我,获得它的代码通过:
new Surface(mPlaybackView.getSurfaceTexture());
mPlaybackView是从资源中获取的TextureView。当调用lockCanvas(null)时,我得到以下内容:
BufferQueueProducer: [unnamed-6903-1] connect(P): already connected (cur=3 req=2)
和
java.lang.IllegalArgumentException at android.view.Surface.nativeLockCanvas (Native Method).
我被难住了,因为我仍在学习画布和表面等
这里有什么明显的问题吗?
编辑:这是堆栈跟踪。
11-19 09:45:28.075 3319-3319/com.example.eschjen.nov15test D/Jenny: inside try, surface is: Surface(name=android.graphics.SurfaceTexture@10e80e42)/@0x3bc8b489
11-19 09:45:28.086 3319-3319/com.example.eschjen.nov15test E/BufferQueueProducer: [unnamed-3319-0] connect(P): already connected (cur=3 req=2)
11-19 09:45:28.087 3319-3319/com.example.eschjen.nov15test E/Jenny: Exception caught:
java.lang.IllegalArgumentException
at android.view.Surface.nativeLockCanvas(Native Method)
at android.view.Surface.lockCanvas(Surface.java:255)
at com.example.eschjen.nov15test.MediaCodecWrapper.surfaceRender(MediaCodecWrapper.java:469)
at com.example.eschjen.nov15test.MediaCodecWrapper.access$200(MediaCodecWrapper.java:41)
at com.example.eschjen.nov15test.MediaCodecWrapper$1.outputSample(MediaCodecWrapper.java:338)
at com.example.eschjen.nov15test.MediaCodecWrapper.popSampleJenny(MediaCodecWrapper.java:345)
at com.example.eschjen.nov15test.MainActivity$1.onTimeUpdate(MainActivity.java:183)
at android.animation.TimeAnimator.animationFrame(TimeAnimator.java:27)
at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1248)
at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:659)
at android.animation.ValueAnimator$AnimationHandler.run(ValueAnimator.java:682)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-19 09:45:28.087 3319-3319/com.example.eschjen.nov15test D/Jenny: done trying
11-19 09:45:28.088 3319-3319/com.example.eschjen.nov15test D/Jenny: done being synchronized.
我认为mPlaybackView
是TextureView
。如果"rect"矩形无效,Surface.lockCanvas(rect)
可以抛出IllegalArgumentException
E/BufferQueueProducer:[未命名-3319-0]连接(p):已连接(cur=3req=2)
来自TextureView
:中的文档
TextureView的SurfaceTexture可以通过调用getSurfaceTexture()或使用TextureView.SurfaceTextureListener。重要的是要知道SurfaceTexture仅在TextureView附加到窗口(而onAttachedToWindow()具有已调用。)因此,强烈建议您使用监听器以在SurfaceTexture可用时得到通知。需要注意的是,只有一个生产者可以使用TextureView例如,如果使用TextureView显示相机预览,则不能同时使用lockCanvas()在TextureView上绘制
您需要确保与此TextureView
关联的SurfaceTexture
可用于渲染。您可以使用TextureView.isAvailable()
进行确认。
我用相同的TextureView
和SurfaceTexture
切换相机。相同的错误already connected
对我来说surface.release
有效。一些代码片段供您参考:
启动相机时执行此操作
if (previewSurfaceTarget == null) previewSurfaceTarget = new Surface(mPreviewSurfaceTexture);
在重新启动之前执行此操作
if (null != mCameraDevice) {
mCameraDevice.close();
mCameraDevice = null;
mCameraIsOpen = false;
}
if (previewSurfaceTarget != null)
previewSurfaceTarget.release();
previewSurfaceTarget = null;