1

I have the following SurfaceView class:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private Camera mCamera;

public CameraPreview(Context context, Camera camera){
    super(context);
    mCamera = camera;
    mCamera.setDisplayOrientation(90);
    //get the holder and set this class as the callback, so we can get camera data here
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);;
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try{
        //when the surface is created, we can set the camera to draw images in this surfaceholder
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("ERROR", "Camera error on surfaceCreated " + e.getMessage());
    }
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    //before changing the application orientation, you need to stop the preview, rotate and then start it again
    if(mHolder.getSurface() == null)//check if the surface is ready to receive camera data
        return;

    try{
        mCamera.stopPreview();
    } catch (Exception e){
        //this will happen when you are trying the camera if it's not running
    }

    //now, recreate the camera preview
    try{
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    //our app has only one screen, so we'll destroy the camera in the surface
    //if you are unsing with more screens, please move this code your activity
    mCamera.stopPreview();
    mCamera.release();
}



}

I currently use it to take pictures from the SurfaceView. Now I want to do the same with videos. I have tried using the mediaRecorder and setting the source to SURFACE however I keep getting on stop failed errors and nobody on here knows why. Can someone please help me create code which I can use to capture video from this surfaceView? This is my previous question regarding the errors I encountered : MediaRecorder stop failed Android

Community
  • 1
  • 1
Alk
  • 5,215
  • 8
  • 47
  • 116

0 Answers0