Arthur
Arthur

Reputation: 789

MediaRecorder: start failed: -19

I'm trying to understand how to record video with camera. But it's always crashes on starting recording. And I don't have any idea why it's happening. I don't need to use any preview for now, just record to a file. Here is the code

    public static final String TAG = "tag";
    Button recBut;
    private MediaRecorder rec;
    private Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        releaseCamera();
        recBut = (Button) findViewById(R.id.rec);

        camera = Camera.open(0);
        prepareCamera(camera);
        recBut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (recBut.getText().toString().equalsIgnoreCase("start")) {
                    rec.start(); //logcat say that problem here
                    recBut.setText("RECING...");
                } else {
                    rec.stop();
                    rec.reset();
                    rec.release();
                    rec = null;
                    recBut.setText("START");
                    releaseCamera();
                }
            }
        });
    }

    private void prepareCamera(Camera camera) {
        List<Camera.Size> list = camera.getParameters().getSupportedVideoSizes();
        Log.i(TAG, "GENERAL SIZE - " + list.size());
        for (int i = 0; i < list.size(); i++) {
            Log.i(TAG, "SIZE| width - " + list.get(i).width + " hight - " + list.get(i).height);
        }
        rec = new MediaRecorder();
        camera.getParameters().setRecordingHint(true);
        rec.setCamera(camera);
        rec.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        rec.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        rec.setOutputFormat(MediaRecorder.OutputFormat.WEBM);
        rec.setVideoSize(720, 1280);
        rec.setVideoFrameRate(24);
        rec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        rec.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        //        rec.setProfile(CamcorderProfile.get(0, CamcorderProfile.QUALITY_HIGH));
        rec.setOutputFile(
                Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_MOVIES + "/test.mp4");
        rec.setPreviewDisplay(null);
        try {
            rec.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void releaseCamera() {
        if (camera != null) {
            camera.release();
            camera = null;
        }
    }
}

Manifest, just in case:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lexz.basiccamera">

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest> 

Logcat:

E/MediaRecorder: start failed: -19
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lexz.basiccamera, PID: 28879
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at com.lexz.basiccamera.MainActivity$1.onClick(MainActivity.java:35)
at android.view.View.performClick(View.java:4802)
at android.view.View$PerformClick.run(View.java:20101)
at android.os.Handler.handleCallback(Handler.java:810)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)

SOLUTION

Looks like for proper work camera class need to have SurfaceView, SurfaceHolder and implement SurfaceHolder.Callback and don't forget to set this surfaceview to camera. As working example check this.

Upvotes: 0

Views: 1886

Answers (2)

Doug Amos
Doug Amos

Reputation: 4383

For me the problem was that I did not unlock the camera. Before setting the camera unlock it:

private void prepareCamera(Camera camera) {
    ...
    camera.unlock();        // Unlock the camera  
    rec.setCamera(camera);
    ...

Upvotes: 1

Master Disaster
Master Disaster

Reputation: 759

rec.setOutputFormat(MediaRecorder.OutputFormat.WEBM);

It is data in a WEBM containe. Try to replace with

 rec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

Upvotes: 0

Related Questions