您的位置:首页 > 移动开发 > Android开发

最近在使用MediaRecorder录制视频的时候遇到crash

2016-07-28 10:09 453 查看
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里面把这些事情都做了,不需要再重复做
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android