Home » Android » Media Recorder start failed in android for camera

Media Recorder start failed in android for camera

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am working on video recording app in which i want to display preview and when user click on record button it start recording and when user click stop button it stop recording.

I got video preview on my surface but when i press start button it crash with error “MEDIA.RECORDER.START(Native MEthod).
Here is my code Please help me guys.

  @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
            surfaceHolder = surfaceView.getHolder();
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            Button start =(Button)findViewById(R.id.start);
            Button stop =(Button)findViewById(R.id.stop);
            stop.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    camera.stopPreview();
                    stopRecording();
                }
            });
            start.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startRecording();
                }
            });
    }

    @Override
public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
    if (camera != null){
        Camera.Parameters params = camera.getParameters();
        camera.setParameters(params);
    }
    else {
        Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
        finish();
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    if (previewRunning){
        camera.stopPreview();
    }
    Camera.Parameters p = camera.getParameters();
     List<Camera.Size> sizes = p.getSupportedPreviewSizes(); 
     Camera.Size cs = sizes.get(0);  
     p.setPreviewSize(cs.width, cs.height);  
       camera.setParameters(p);

    try {
        camera.setPreviewDisplay(holder);
        camera.startPreview();
        previewRunning = true;
    }
    catch (IOException e) {
        Log.e(TAG,e.getMessage());
        e.printStackTrace();
    }
}

 private MediaRecorder mediaRecorder;
    private final int maxDurationInMs = 20000;
    private final long maxFileSizeInBytes = 500000;
    private final int videoFramesPerSecond = 20;

    public boolean startRecording(){
        try {
            camera.unlock();

            mediaRecorder = new MediaRecorder();

            mediaRecorder.setCamera(camera);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

            mediaRecorder.setMaxDuration(maxDurationInMs);

        File tempFile = new File(getCacheDir(),"test.mp4");
            mediaRecorder.setOutputFile(tempFile.getPath());

            mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
            mediaRecorder.setVideoSize(surfaceView.getWidth(), surfaceView.getHeight());

            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

            mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

            mediaRecorder.setMaxFileSize(maxFileSizeInBytes);

                        mediaRecorder.prepare();
            mediaRecorder.start();

            return true;
        } catch (IllegalStateException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            Log.e(TAG,e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    public void stopRecording(){
        mediaRecorder.stop();
        camera.lock();
    }

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    previewRunning = false;
    camera.release();
}

}

Log is

 08-31 02:20:11.781: E/MediaRecorder(14519): start failed: -19
 08-31 02:20:11.781: D/AndroidRuntime(14519): Shutting down VM
 08-31 02:20:11.781: W/dalvikvm(14519): threadid=1: thread exiting with uncaught exception (group=0x416c9700)
 08-31 02:20:11.781: E/AndroidRuntime(14519): FATAL EXCEPTION: main
 08-31 02:20:11.781: E/AndroidRuntime(14519): java.lang.RuntimeException: start failed.
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.media.MediaRecorder.start(Native Method)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at com.example.cameratest.MainActivity.startRecording(MainActivity.java:135)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at com.example.cameratest.MainActivity$2.onClick(MainActivity.java:61)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.view.View.performClick(View.java:4240)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.view.View$PerformClick.run(View.java:17721)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.os.Handler.handleCallback(Handler.java:730)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.os.Handler.dispatchMessage(Handler.java:92)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.os.Looper.loop(Looper.java:137)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at android.app.ActivityThread.main(ActivityThread.java:5103)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at java.lang.reflect.Method.invokeNative(Native Method)
08-31 02:20:11.781: E/AndroidRuntime(14519):    at java.lang.reflect.Method.invoke(Method.java:525)
08-31 02:20:11.781: E/AndroidRuntime(14519):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-31 02:20:11.781: E/AndroidRuntime(14519):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 08-31 02:20:11.781: E/AndroidRuntime(14519):   at dalvik.system.NativeStart.main(Native Method)
How to&Answers:

I Found This solution and its working fine for me and solved this problem 🙂

for 2.3+ android:

Just change:

mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

to:

mediaRecorder.setProfile(CamcorderProfile.get(CameraID,CamcorderProfile.QUALITY_HIGH));

Answer:

in your startRecording method try to lock camera before you unlock it:

mediaRecorder = new MediaRecorder();
camera.lock();
camera.unlock();

Answer:

comment this line of code,

mediaRecorder.setVideoSize(surfaceView.getWidth(), surfaceView.getHeight());

Answer:

I have solved the start failed error -19 by changing the videoFramesPerSecond value as 30. which is the recommended value for
QUALITY_1080p-Quality level corresponding to the 1080p (1920 x 1080) resolution and QUALITY_480P- Quality level corresponding to the 480p (720 x 480) resolution.

source: http://developer.android.com/guide/appendix/media-formats.html

Answer:

Try following may be it work : help to anybody :

        try {
            mediaRecorder.prepare();
            Thread.sleep(1000);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        mediaRecorder.start();

For incoming call it was taking time to prepare.

Hold the prepare for 1 sec and everything is worked fine.

Answer:

This may be a lot less complicated than you think. Android has done a lot of the leg work for something as simple as recording and returning the video file by using Intents

Record a Video with a Camera App
The Android way of delegating actions to other applications is to invoke an Intent that describes what you want done. This involves three pieces: the Intent itself, a call to start the external Activity, and some code to handle the video when focus returns to your activity.

Here’s a function that invokes an intent to capture video.

private void dispatchTakeVideoIntent() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(takeVideoIntent, ACTION_TAKE_VIDEO);
}

View the Video
The Android Camera application returns the video in the Intent delivered to onActivityResult() as a Uri pointing to the video location in storage. The following code retrieves this video and displays it in a VideoView.

private void handleCameraVideo(Intent intent) {
mVideoUri = intent.getData();
mVideoView.setVideoURI(mVideoUri);
}

Add this to the Manifest

<uses-feature android:name="android.hardware.camera" />

source: http://developer.android.com/training/camera/videobasics.html