Android MediaRecorder - "启动失败:-19"

时间:2012-05-08 10:38:20

标签: android android-camera android-mediarecorder

我正在尝试在Android上创建一个录像机,并且我已准备好应该正常工作的代码 - 但我不断收到错误消息start failed: -19

这是我的代码:

public boolean startRecording() {
    try {
        camera.unlock();
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {

                @Override
                public void onError(MediaRecorder mr, int what, int extra) {
                Log.i(TAG, "Error");
            }
        });

        mediaRecorder.setCamera(camera);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        Log.i(TAG, "a");

        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
        Log.i(TAG, "b");

        mediaRecorder.setMaxDuration(maxDurationInMs); // set to 20000

        String uniqueOutFile = OUTPUT_FILE + System.currentTimeMillis() + ".3gp";
        File outFile = new File(uniqueOutFile);
        if (outFile.exists()) {
            outFile.delete();
        }
        mediaRecorder.setOutputFile(uniqueOutFile);
        mediaRecorder.setVideoFrameRate(videoFramesPerSecond); // set to 20
        mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight());
        Log.i(TAG, "c");

        mediaRecorder.setPreviewDisplay(holder.getSurface());
        mediaRecorder.setMaxFileSize(maxFileSizeInBytes); // set to 50000
        mediaRecorder.prepare();
        Log.i(TAG, "d");

        mediaRecorder.start();
        Log.i(TAG, "e");

        return true;
        } catch (IllegalStateException e) {
            Log.i(TAG, "f");
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
            camera.lock();
            return false;
        } catch (IOException e) {
            Log.i(TAG, "g");
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
            camera.lock();
            return false;
        } catch (RuntimeException e) {
            Log.i(TAG, "h");
            Log.e(TAG, e.getMessage());
            camera.lock();
            return false;
        }
    }

所有调试日志(从“a”到“d”)都打印在日志中,因此看起来mediaRecorder.prepare()之前的所有步骤都已正确完成。然后它会抓取RuntimeException消息start failed: -19。有类似的question,但这并不能解决我的问题。

有没有其他原因可以得到这样的错误?

5 个答案:

答案 0 :(得分:14)

刚刚发现了这个错误,在这一行:

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

在评论此行后,代码运行完美!

答案 1 :(得分:2)

一旦我将其添加到视频录制中,我解决了我的问题

/**
 * 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;
    }
}

有关详细指南,请参阅此Open Source Cuxtom Cam

答案 2 :(得分:0)

问题出在您的setVideoSize()代码中。

以及此代码错误的原因......

根据我所做的研究,当MediaRecorder#setVideoSize()

设置的视频大小出现问题时,会出现错误代码-19

运行此代码,并查看设备中的相机可以支持的whitch屏幕:

final List<Camera.Size> mSupportedVideoSizes = getSupportedVideoSizes(mCamera);
        for (Camera.Size str : mSupportedVideoSizes)
            Log.e(TAG, "mSupportedVideoSizes "+str.width + ":" + str.height + " ... "
                    + ((float) str.width / str.height));

和方法是:

public List<Size> getSupportedVideoSizes(Camera camera) {
        if (camera.getParameters().getSupportedVideoSizes() != null) {
            return camera.getParameters().getSupportedVideoSizes();
        } else {
            // Video sizes may be null, which indicates that all the supported 
            // preview sizes are supported for video recording.
            return camera.getParameters().getSupportedPreviewSizes();
        }
    }

答案 3 :(得分:0)

我遇到了某些特定手机的问题,我发现我无法在其中一些手机中设置camcoder配置文件尺寸。但是当它对有问题的机器人起作用时,它就不再使用以前的工作设备了。

所以最后我实现的逻辑是这样的:

  • 设置宽度/高度
  • 尝试启动merdia录音机
  • 如果发生异常,请在不设置宽度/高度的情况下再试一次

有点垃圾逻辑,但是有效。

我已经使用该实现设置了一个github项目,请尝试一下:https://github.com/rafaelsilverio/MediaRecorder

答案 4 :(得分:0)

我也遇到了这个问题并注释了以下两种方式,因为硬件不支持这两种配置。

MediaRecorder .setVideoSize()
MediaRecorder .setVideoFrameRate()