解决Android MediaRecorder录制视频过短问题
具体表现:
调用MediaRecorder的start()与stop()间隔不能小于1秒(有时候大于1秒也崩),否则必崩。
错误信息:
java.lang.RuntimeException:stopfailed. atandroid.media.MediaRecorder.stop(NativeMethod)
解决办法:
在stop以前调用setOnErrorListener(null);就行了!
相关代码:
/**开始录制*/
@Override
publicMediaPartstartRecord(){
if(mMediaObject!=null&&mSurfaceHolder!=null&&!mRecording){
MediaPartresult=mMediaObject.buildMediaPart(mCameraId,".mp4");
try{
if(mMediaRecorder==null){
mMediaRecorder=newMediaRecorder();
mMediaRecorder.setOnErrorListener(this);
}else{
mMediaRecorder.reset();
}
//Step1:UnlockandsetcameratoMediaRecorder
camera.unlock();
mMediaRecorder.setCamera(camera);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
//Step2:Setsources
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//beforesetOutputFormat()
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//beforesetOutputFormat()
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//设置视频输出的格式和编码
CamcorderProfilemProfile=CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
//mMediaRecorder.setProfile(mProfile);
mMediaRecorder.setVideoSize(640,480);//aftersetVideoSource(),aftersetOutFormat()
mMediaRecorder.setAudioEncodingBitRate(44100);
if(mProfile.videoBitRate>2*1024*1024)
mMediaRecorder.setVideoEncodingBitRate(2*1024*1024);
else
mMediaRecorder.setVideoEncodingBitRate(mProfile.videoBitRate);
mMediaRecorder.setVideoFrameRate(mProfile.videoFrameRate);//aftersetVideoSource(),aftersetOutFormat()
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);//aftersetOutputFormat()
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//aftersetOutputFormat()
//mMediaRecorder.setVideoEncodingBitRate(800);
//Step4:Setoutputfile
mMediaRecorder.setOutputFile(result.mediaPath);
//Step5:Setthepreviewoutput
//mMediaRecorder.setOrientationHint(90);//加了HTC的手机会有问题
Log.e("Yixia","OutputFile:"+result.mediaPath);
mMediaRecorder.prepare();
mMediaRecorder.start();
mRecording=true;
returnresult;
}catch(IllegalStateExceptione){
e.printStackTrace();
Log.e("Yixia","startRecord",e);
}catch(IOExceptione){
e.printStackTrace();
Log.e("Yixia","startRecord",e);
}catch(Exceptione){
e.printStackTrace();
Log.e("Yixia","startRecord",e);
}
}
returnnull;
}
/**停止录制*/
@Override
publicvoidstopRecord(){
longendTime=System.currentTimeMillis();
if(mMediaRecorder!=null){
//设置后不会崩
mMediaRecorder.setOnErrorListener(null);
mMediaRecorder.setPreviewDisplay(null);
try{
mMediaRecorder.stop();
}catch(IllegalStateExceptione){
Log.w("Yixia","stopRecord",e);
}catch(RuntimeExceptione){
Log.w("Yixia","stopRecord",e);
}catch(Exceptione){
Log.w("Yixia","stopRecord",e);
}
}
if(camera!=null){
try{
camera.lock();
}catch(RuntimeExceptione){
Log.e("Yixia","stopRecord",e);
}
}
mRecording=false;
}
/**释放资源*/
@Override
publicvoidrelease(){
super.release();
if(mMediaRecorder!=null){
mMediaRecorder.setOnErrorListener(null);
try{
mMediaRecorder.release();
}catch(IllegalStateExceptione){
Log.w("Yixia","stopRecord",e);
}catch(Exceptione){
Log.w("Yixia","stopRecord",e);
}
}
mMediaRecorder=null;
}
@Override
publicvoidonError(MediaRecordermr,intwhat,intextra){
try{
if(mr!=null)
mr.reset();
}catch(IllegalStateExceptione){
Log.w("Yixia","stopRecord",e);
}catch(Exceptione){
Log.w("Yixia","stopRecord",e);
}
if(mOnErrorListener!=null)
mOnErrorListener.onVideoError(what,extra);
}
以上就是对AndroidMediaRecorder资料整理,后续继续补充,有需要的朋友可以参考下。