Mediarecorder start failed -19

2020-02-11 07:40发布

I am getting this error when running start() for mediarecorder.

06-28 18:46:22.570: E/MediaRecorder(9540): start failed: -19
06-28 18:46:22.570: W/System.err(9540): java.lang.RuntimeException: start failed.

I am extending mediarecorder class
My code:

camera = Camera.open(cameraId);
super.setCamera(camera);
        super.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        super.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        if (mode==MODE_DEFAULT) {
            super.setMaxDuration(1000);
            super.setMaxFileSize(Integer.MAX_VALUE);
        } else {
            // On some phones a RuntimeException might be thrown :/
            try {
                super.setMaxDuration(0);
                super.setMaxFileSize(Integer.MAX_VALUE); 
            } catch (RuntimeException e) {
                Log.e(TAG,"setMaxDuration or setMaxFileSize failed !");
            }
        }
        super.setVideoEncoder(videoEncoder);
        if(surfaceHolder!=null)
        super.setPreviewDisplay(surfaceHolder.getSurface());
        //super.setVideoSize(quality.resX,quality.resY);
        super.setVideoFrameRate(quality.frameRate);
        super.setVideoEncodingBitRate(quality.bitRate);

I saw these pages
Error opening android camera for streaming video
Android MediaRecorder - "start failed: -19"
But non of them worked for me...
Running on archos 80 g9, android 3.2 Any one got any ideas?

5条回答
不美不萌又怎样
2楼-- · 2020-02-11 07:50

This code worked for me (found here)

mCamera.unlock(); // maybe not for your activity flow

//1st. Initial state
mProfile = CamcorderProfile.get( CamcorderProfile.QUALITY_HIGH );
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera( mCamera );

//2nd. Initialized state
mMediaRecorder.setAudioSource( MediaRecorder.AudioSource.CAMCORDER );
mMediaRecorder.setVideoSource( MediaRecorder.VideoSource.CAMERA );

//3rd. config
mMediaRecorder.setOutputFormat( mProfile.fileFormat );
mMediaRecorder.setAudioEncoder( mProfile.audioCodec );
mMediaRecorder.setVideoEncoder( mProfile.videoCodec );
mMediaRecorder.setOutputFile( "/sdcard/FBVideo.3gp" );
mMediaRecorder.setVideoSize( mProfile.videoFrameWidth, mProfile.videoFrameHeight );
mMediaRecorder.setVideoFrameRate( mProfile.videoFrameRate );
mMediaRecorder.setVideoEncodingBitRate( mProfile.videoBitRate );
mMediaRecorder.setAudioEncodingBitRate( mProfile.audioBitRate );
mMediaRecorder.setAudioChannels( mProfile.audioChannels );
mMediaRecorder.setAudioSamplingRate( mProfile.audioSampleRate );
mMediaRecorder.setPreviewDisplay( mHolder.getSurface() );

try {
    mMediaRecorder.prepare();
    mMediaRecorder.start();
} catch ( IllegalStateException e ) {
    e.printStackTrace();
} catch ( IOException e ) {
    e.printStackTrace();
}
查看更多
够拽才男人
3楼-- · 2020-02-11 07:52

I was facing the same proble during video recording and i solved it by adding this for video recording

/**
 * Start video recording by cleaning the old camera preview
 */
private void startVideoRecorder() {
    // THIS IS NEEDED BECAUSE THE GLASS CURRENTLY THROWS AN ERROR OF
    // "MediaRecorder start failed: -19"
    // THIS WONT BE NEEDED INCASE OF PHONE AND TABLET
    // This causes crash in glass kitkat version so remove it
    // try {
    // mCamera.setPreviewDisplay(null);
    // } catch (java.io.IOException ioe) {
    // Log.d(TAG,
    // "IOException nullifying preview display: "
    // + ioe.getMessage());
    // }
    // mCamera.stopPreview();
    // mCamera.unlock();
    recorder = new MediaRecorder();
    // Let's initRecorder so we can record again
    initRecorder();
}

/**
 * Initialize video recorder to record video
 */
private void initRecorder() {
    try {
        File dir = new File(folderPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        mCamera.stopPreview();
        mCamera.unlock();
        videofile = new File(dir, fileName + ".mp4");
        recorder.setCamera(mCamera);

        // Step 2: Set sources
        recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
        recorder.setProfile(CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH));

        // Step 4: Set output file
        recorder.setOutputFile(videofile.getAbsolutePath());
        // Step 5: Set the preview output
        recorder.setPreviewDisplay(mPreview.getHolder().getSurface());
        // Step 6: Prepare configured MediaRecorder
        recorder.setMaxDuration(video_duration * 1000);
        recorder.setOnInfoListener(new OnInfoListener() {

            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {
                if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {

                    mCamera.stopPreview();
                    releaseMediaRecorder();

                    /*
                     * initiate media scan and put the new things into the
                     * path array to make the scanner aware of the location
                     * and the files you want to see
                     */MediaScannerConnection.scanFile(
                            CuxtomCamActivity.this,
                            new String[] { videofile.getPath() }, null,
                            null);

                    Intent intent = new Intent();
                    intent.putExtra(CuxtomIntent.FILE_PATH,
                            videofile.getPath());
                    intent.putExtra(CuxtomIntent.FILE_TYPE, FILE_TYPE.VIDEO);
                    setResult(RESULT_OK, intent);
                    finish();
                }

            }
        });
        recorder.prepare();
        recorder.start();
    } catch (Exception e) {
        Log.e("Error Stating CuXtom Camera", e.getMessage());
    }
}
private void releaseMediaRecorder() {
    if (recorder != null) {
        recorder.reset(); // clear recorder configuration
        recorder.release(); // release the recorder object
        recorder = null;
    }
}

To see detail of how a camera is actually implemented refer to Open Source Cuxtom Cam

查看更多
Explosion°爆炸
4楼-- · 2020-02-11 07:54

Fixed by removing

super.setVideoFrameRate(quality.frameRate);
查看更多
再贱就再见
5楼-- · 2020-02-11 08:01

The problem here is about the camera. Just use camera.unlock() to allow the media process to access the camera.

This must be done before calling MediaRecorder.setCamera(Camera). This cannot be called after recording starts.

Read more here.

查看更多
做自己的国王
6楼-- · 2020-02-11 08:02

I found a subtle hint in documentation for the MediaRecorder.start() method that suggest if it fails to lock() the Camera before attempting to re-record. This worked for me. Implies a Camera state bug was fixed post API level 13 - calling Camera.lock() is the known workaround.

查看更多
登录 后发表回答