我收到 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)