Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera preview freezes after screen lock

The custom camera app I've written stops giving the preview after the screen locks (by pushing lock butten or waiting for a couple of minutes). I don't get an exception, which makes it quite difficult to find the problem.

Does the android screen lock (if that's the correct term) pauses/halts/... my App (activity)?

If this were the case, could the cause be my onPause/onResume methods? Or is another cause mor likely?

Thanks in advance

like image 329
Hendrik De Blanger Avatar asked May 06 '13 10:05

Hendrik De Blanger


3 Answers

I faced same problem and fixed it using following steps:

  1. I created my camera preview and added it to the container FrameLayout in onResume() of the parent activity. Something like:

    public void onResume{
        super.onResume();
        mCamera = Camera.open();
        if(null != mCamera){
            mCamera.setDisplayOrientation(90);
            mPreview = new CameraOverlay(getActivity(), mCamera);
            frLyParent.addView(mPreview);
        }
    }
    
  2. I removed the view in onPause(). This fixes the freeze.

    public void onPause(){
        super.onPause();
        if(null != mCamera){
            mCamera.release();
            mCamera = null;
        }
        frLyParent.removeView(mPreview);
        mPreview = null;
    }
    

where CameraOverlay() is the class which extends SurfaceView and implements SurfaceHolder.Callback. Do let me know if you need that implementation.

like image 144
2 revs Avatar answered Nov 01 '22 20:11

2 revs


here is mine, it does work :-)

@Override
    public void onResume() {
        super.onResume();
        try {
            camera = Camera.open();
            holder.addCallback(this);
            surface.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onPause() {
        try {
            surface.setVisibility(View.GONE);
            holder.removeCallback(this);
            camera.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onPause();
    }
like image 7
Afrig Aminuddin Avatar answered Nov 01 '22 22:11

Afrig Aminuddin


In onResume() method add this line :

surfaceView.setVisibility(View.VISIBLE);

In onPaused() method add this line :

surfaceView.setVisibility(View.GONE);
like image 5
Hantash Nadeem Avatar answered Nov 01 '22 22:11

Hantash Nadeem