Camera onPreviewFrame not called

15,568

Solution 1

You must call setPreviewCallback in the surfaceChanged method, not only in the surfaceCreated.

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null){
  return;
}

try {
    mCamera.stopPreview();
} catch (Exception e){
  // ignore: tried to stop a non-existent preview
}

try {
    mCamera.setPreviewCallback(this);
    mCamera.setPreviewDisplay(mHolder);
    mCamera.startPreview();

} catch (Exception e){
    Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}

Solution 2

I had a similar problem; see

setOneShotPreviewCallback not hitting onPreviewFrame() in callback

What I discovered was that after calling Camera#unlock() to prepare the MediaRecorder, it was necessary to call Camera#reconnect() before setting the preview callback. This is because Camera.unlock() detaches the camera from the process to let the MediaRecorder connect to it.

http://developer.android.com/reference/android/hardware/Camera.html#unlock()

In my investigations I also discovered that if you set any preview callbacks using other methods than the one shot method, you have to reset all of these after calling Camera#reconnect() as well. So, briefly:

mCamera.unlock();
//set up MediaRecorder
mCamera.reconnect();
mCamera.setPreviewCallback(mCallback);
//or whatever callback method you want to use
//and even if you've set this callback already

I hope that helps!

Solution 3

You should call it within new instantiation of previewCallBacks() interface, like below

public void surfaceCreated(SurfaceHolder holder) {
        // if (mediaRecorder == null) {
        try {
            camera = Camera.open();
            camera.setPreviewCallback(new PreviewCallback() {
                public void onPreviewFrame(byte[] _data, Camera _camera) {
                }
            }
        }
}
Share:
15,568
esse
Author by

esse

Updated on June 30, 2022

Comments

  • esse
    esse almost 2 years

    when using the Camera.PreviewCallback implementation the onPreviewFrame is called without problem after initializing camera and starting preview (Camera.startPrevew()). The problem is if I make a video recording using MediaRecorder onPreviewFrame does not get called any more.

    I understand that when using MediaRecorder to record video stops the Camera.PreviewCallback, but why can't it be restarted?

    I have tried resetting the camera preview callback (setPreviewCallback(callback)) and restarting startPreview, but while I have a preview there is no call to onPreviewFrame.