siavash
siavash

Reputation: 15

Android Camera records well but startPreview fails

I am developing an android app which records video within app. it records the video well and saves in file. but the startpreview function fails and I have no preview during recording.

this is the code :

private  void startVRec(){
        //final View view = inflater.inflate(R.layout.fragment_conversation, container, false);
        try {

            vrec = new MediaRecorder();
            mCamera = Camera.open();
             params = mCamera.getParameters();
            Camera.Size size=getOptimalPreviewSize(params.getSupportedPreviewSizes(), 240, 240);
            params.setPreviewSize(size.height, size.width);
            mCamera.setParameters(params);
            vrec.setPreviewDisplay(surfaceHolder.getSurface());
           mCamera.unlock();
            vrec.setCamera(mCamera);
            camera_layout.setVisibility(View.VISIBLE);
            surfaceView.setVisibility(View.VISIBLE);
        } catch (Exception e) {

        }


    }

as I saw in the most examples in internet , the startpreview function is called on surfaceChanged event:

 @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                   int height) {

            //Camera.Size previewSize=null;
            try {

                if (mCamera != null)
                {
                    Camera.Size 
                   size=getOptimalPreviewSize(params.getSupportedPreviewSizes(), 240, 240);
                    params.setPreviewSize(size.height, size.width);
                    mCamera.setParameters(params);
                    mCamera.startPreview();
                }
            } catch (Exception e) {
                Toast.makeText(activity, "error "+ e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }

And it starts recording on surfaceCreated event

public void surfaceCreated(SurfaceHolder holder) { if (mCamera != null){

        try {

            vrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            vrec.setAudioSource(MediaRecorder.AudioSource.MIC);
            vrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            vrec.setVideoFrameRate(30);
            vrec.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
            vrec.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            vrec.setPreviewDisplay(surfaceHolder.getSurface());
            vrec.setOutputFile(FileBackend.getConversationsImageDirectory() + System.currentTimeMillis() + ".mp4");
            vrec.prepare();

            vrec.start();

        }
       catch(IllegalStateException e2){
                Toast.makeText(activity,"err cam stop ill :"+ e2.getMessage(),Toast.LENGTH_LONG).show();
            }

         catch (Exception e) {
            Toast.makeText(activity,"err surface created :"+ e.getMessage(),Toast.LENGTH_LONG).show();
        }

    }
    else {
        Toast.makeText(activity, "Camera not available!", Toast.LENGTH_LONG).show();

    }
}

Upvotes: 1

Views: 100

Answers (1)

nhoxbypass
nhoxbypass

Reputation: 10152

Try setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) for your holder of camera SurfaceView

Upvotes: 0

Related Questions