How to recover camera preview from sleep?

后端 未结 2 1787
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 01:48

I have an application that shows camera preview and I would like the user to be able to put the phone to sleep and then wake it so that my application will recover correctly

相关标签:
2条回答
  • 2020-12-18 02:21

    I was having the same issue and here's why:

    The onPause method does not recycle the CameraPreview class I made (that implements SurfaceView Callbacks). Instead of trying to re instantiate that entire object itself, i merely updated the camera object reference i was passing it to be either

    null
    

    or

    cameraPreview.setCamera(mCamera);
    

    I called a getCameraInstance method to re initialize the camera, then passed it into the preivew object.

    Now the problem is here:

    private void initializeCameraView(){
    
        RelativeLayout preview = (RelativeLayout)rootView.findViewById(R.id.camera_preview);
    
        preview.addView(cameraPreview);
    }
    

    in onResume, i called this method to re initialize the cameraPreview object. However, it was freezing because I was trying to add another cameraPreview to the preview view. This was my fix, plain and simple. If it already exists, remove it, then put it right back in. Hope this helps!

    private void initializeCameraView(){
    
        RelativeLayout preview = (RelativeLayout)rootView.findViewById(R.id.camera_preview);
        //removes the problem with the camera freezing onResume
        if(cameraPreview != null) {
            preview.removeView(cameraPreview);
        }
        preview.addView(cameraPreview);
    }
    
    0 讨论(0)
  • 2020-12-18 02:27

    One solution maybe setting the surfaceview to invisible and visible again in onResume(), this makes the surfaceview destroy and recreates.

    0 讨论(0)
提交回复
热议问题