使用相机录制时会抛出异常

时间:2011-06-22 08:08:35

标签: android android-camera

当相机开始记录我的应用程序时会抛出此异常:

E/MediaRecorder( 3316): setOutputFile called in an invalid state(2):
E/AndroidRuntime( 3316): java.lang.IllegalStateException
E/AndroidRuntime( 3316):        at android.media.MediaRecorder._setOutputFile(Native Method)
E/AndroidRuntime( 3316):        at android.media.MediaRecorder.prepare(MediaRecorder.java:542)
E/AndroidRuntime( 3316):        at com.android.cameradiag.VideoCamera.initializeRecorder(VideoCamera.java:448)
E/AndroidRuntime( 3316):        at com.android.cameradiag.VideoCamera.startVideoRecording(VideoCamera.java:638)

我的代码是:

mMediaRecorder = new MediaRecorder();

// Unlock the camera object before passing it to media recorder.
mCameraDevice.unlock();
mMediaRecorder.setCamera(mCameraDevice);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// mMediaRecorder.setProfile(mProfile);
mMediaRecorder.setMaxDuration(mMaxVideoDurationInMs);

// Set output file.
if (mStorageStatus != STORAGE_STATUS_OK) {
    mMediaRecorder.setOutputFile("/dev/null");
} else {
    // Try Uri in the intent first. If it doesn't exist, use our own
    // instead.

    createVideoPath();
    mMediaRecorder.setOutputFile(mVideoFilename);

}

mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
try {
    mMediaRecorder.prepare();
} catch (IOException e) {
    Log.e(TAG, "prepare failed for " + mVideoFilename, e);
    releaseMediaRecorder();
    throw new RuntimeException(e);
}

我发现SDCard上存在记录的3gp文件,但它的大小为0字节。

1 个答案:

答案 0 :(得分:5)

在使用mMediaRecorder.setOutputFile(mVideoFilename);之前,您必须设置输出格式。

请参阅MediaRecorder上的流程图​​。