Mediarecorder开始失败 - 19

时间:2015-10-04 08:03:31

标签: android camera android-camera front-camera

我正在使Mediarecorder启动失败 - 在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)

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

我按照以下提示解决了这个问题:1。在构建中清理我的项目; 2.我将setAudioSamplingRate(10)更改为setAudioSamplingRate(8000),可能它的记录速率太小