SurfaceView with camera preview is not destroyed
Asked Answered
I

3

6

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?

Isomorphism answered 24/11, 2011 at 12:47 Comment(1)
Did you call both .stopPreview() and .release() on your camera object?Pfister
I
7

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.

Isomorphism answered 7/3, 2014 at 6:40 Comment(0)
I
3
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;
    }
Isomorphism answered 25/11, 2011 at 6:56 Comment(2)
Found the solution. First of all for each tabbed activity create a framelayout which contains all the child views. Set it as content view. Yet stop camera by simply calling in onPause() setVisibility to View.GONE and View.Visible in onResume().Isomorphism
Thanks a lot for this. I had the problem that on the Sony Ericsson Xperia mini the USB enable/disable menu from the dropdown notification bar paused my camera app but did not destroy the surface. Amazingly only the enable/disable menu had this issue, the USB debug settings menu behaved properly. Using setVisibility() as you described directly for the SurfaceView for the camera preview solved the problem completely.Sedgewinn
G
0

this is OK for me:

 public void surfaceDestroyed(SurfaceHolder holder) {
        Log.e("TABACT", "surfaceDestroyed()");
        camera.stopPreview();
        camera.setPreviewCallback(null); 
        camera.release();
        camera = null;
    }
Gerome answered 17/2, 2015 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.