SurfaceView with camera preview is not destroyed

风格不统一 提交于 2020-01-01 10:48:12

问题


I have a Tab Activity with 2 tabs (activities). Each tab has a 3d Open GL scene drawn on top of a SurfaceView with camera preview.

Yet, depending on device orientation, tabs are being switched.

The problem is that when the other activity starts, it calls camera.open(), which generates exception, saying that camera service is unavailable.

In fact, the problem is that camera is not stopped when activity is paused, in other words onSurfaceDestroyed() is not called for the SurfaceView. I tried to stop camera when onPause() for activities is called, but get the same error still.

Anyone had same issues with tabbed activities? Any idea how to make surfaceview get destroyed?


回答1:


It looks like this question is quite popular, so I'm adding the solution here one more time.

The root cause was that surfaceDestroyed was never called for SurfaceView when app was paused.

So I've created a framelayout which contains all the child views. Set it as content view. Yet stop camera by simply calling setVisibility(View.GONE) in onPause() and View.Visible in onResume(). This will lead to destruction of SurfaceView.




回答2:


private SurfaceHolder.Callback mSurfaceHolderListener = new SurfaceHolder.Callback() {

    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.e("TABACT", "surfaceDestroyed()");
        camera.stopPreview();
        camera.setPreviewCallback(null); 
        camera.release();
        camera = null;
    }



回答3:


this is OK for me:

 public void surfaceDestroyed(SurfaceHolder holder) {
        Log.e("TABACT", "surfaceDestroyed()");
        camera.stopPreview();
        camera.setPreviewCallback(null); 
        camera.release();
        camera = null;
    }


来源:https://stackoverflow.com/questions/8257318/surfaceview-with-camera-preview-is-not-destroyed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!