0

我收到 Mediarecorder start failed - 为 MediaRecorder 运行 start() 时出现 19 错误,仅当我尝试使用以下代码将摄像头设置为从前置摄像头捕获时才会发生这种情况:

    private Camera openFrontFacingCamera()
{
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                Log.e("my tag", "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}

这是我的 startRecording 功能:

    public void startRecording() throws IOException
{
    mCamera = openFrontFacingCamera();
    mrec = new MediaRecorder();  // Works well
    mCamera.unlock();

    mrec.setCamera(mCamera);

    mrec.setPreviewDisplay(surfaceHolder.getSurface());
    mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mrec.setAudioSource(MediaRecorder.AudioSource.MIC);

    mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    mrec.setPreviewDisplay(surfaceHolder.getSurface());

    //Toast.makeText(this,Integer.toString(Date),Toast.LENGTH_SHORT).show();


    String time = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/" + getString(R.string.app_name)+ "/" + DateFormat.getDateTimeInstance().format(new Date()).trim() + ".3gp";

    File directory = new File(time).getParentFile();
    if (!directory.exists() && !directory.mkdirs()) {
        throw new IOException("Path to file could not be created.");
    }

    mrec.setOutputFile(time);

    mrec.prepare();
    mrec.start();
}

和错误:

10-04 10:52:29.488 15546-15546/com.didi.***** E/AndroidRuntime: java.lang.RuntimeException: start failed.
10-04 10:52:29.488 15546-15546/com.didi.***** E/AndroidRuntime:at android.media.MediaRecorder.start(Native Method)
4

2 回答 2

0

我按照以下提示解决了这个问题: 1.在构建中清理我的项目;2.我把setAudioSamplingRate(10)改成setAudioSamplingRate(8000),可能是采样率太小无法记录

于 2016-12-14T02:55:50.587 回答
0

您可以在mCamera.unlock()之前尝试以下代码。

 try {
    mCamera.setPreviewDisplay(null);
 } catch (java.io.IOException e) {

 }
 mCamera.stopPreview();

并尝试以下代码而不是mrec.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mrec.setMaxDuration(VIDEO_DURATION);
mrec.setVideoFrameRate(VIDEO_FRAME_RATE);
mrec.setVideoEncodingBitRate(VIDEO_ENCODING_BIT_RATE);
mrec.setVideoSize(VIDEO_SIZE_WIDth, VIDEO_SIZE_HEIGHT);
于 2015-10-12T07:15:42.487 回答