2015-10-04 135 views
0

我越來越Mediarecorder啓動失敗 - 運行start()方法用於MediaRecorder時19錯誤,這種情況只有當我嘗試設置相機前置攝像頭捕捉到與此代碼:Mediarecorder啓動失敗 - 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) 

回答

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); 
0

我解決了這個問題,下面的提示:1.清理我的項目建立; 2.將setAudioSamplingRate(10)更改爲setAudioSamplingRate(8000),可能對記錄的速率太小

相關問題