recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置视频源
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P));
//设置输出格式
recorder.setOutputFormat(OUTPUT_FORMAT);
recorder.setAudioEncoder(AUDIO_ENCODER);
//设置视频编码
recorder.setVideoEncoder(VIDEO_ENCODER);
// 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错
recorder.setVideoSize(width, height);
// 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错
recorder.setVideoFrameRate(frameRate);
recorder.setOrientationHint(90);
recorder.setPreviewDisplay(surfaceHolder.getSurface());
recorder.setOutputFile("/sdcard/v.mp4");
//disable this limit
recorder.setMaxDuration(0);
recorder.setMaxFileSize(0);
recorder.setOnErrorListener(this);
recorder.setOnInfoListener(this);
在没有setProfile的时候能正常工作,但是视频质量不行,
recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P));
于是报出
setOutputFormat called in an invalid state
后来在stackoverflow找到答案
http://stackoverflow.com/questions/21632769/setoutputformat-called-in-an-invalid-state-4-where-and-why
public void setProfile(CamcorderProfile profile) {
setOutputFormat(profile.fileFormat);
setVideoFrameRate(profile.videoFrameRate);
setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
setVideoEncodingBitRate(profile.videoBitRate);
setVideoEncoder(profile.videoCodec);
if (profile.quality >= CamcorderProfile.QUALITY_TIME_LAPSE_LOW &&
profile.quality <= CamcorderProfile.QUALITY_TIME_LAPSE_QVGA) {
// Nothing needs to be done. Call to setCaptureRate() enables
// time lapse video recording.
} else {
setAudioEncodingBitRate(profile.audioBitRate);
setAudioChannels(profile.audioChannels);
setAudioSamplingRate(profile.audioSampleRate);
setAudioEncoder(profile.audioCodec);
}
}
原来在setProfile里面把这些事情都做了,不需要再重复做