具体表现:
调用MediaRecorder的start()与stop()间隔不能小于1秒(有时候大于1秒也崩),否则必崩。
错误信息:
解决办法:
在stop以前调用setonErrorListener(null);就行了!
相关代码:
/** 开始录制 */ @Override public MediaPart startRecord() { if (mMediaObject != null && mSurfaceHolder != null && !mRecording) { MediaPart result = mMediaObject.buildMediaPart(mCameraId,".mp4"); try { if (mMediaRecorder == null) { mMediaRecorder = new MediaRecorder(); mMediaRecorder.setonErrorListener(this); } else { mMediaRecorder.reset(); } // Step 1: Unlock and set camera to MediaRecorder camera.unlock(); mMediaRecorder.setCamera(camera); mMediaRecorder.setPreviewdisplay(mSurfaceHolder.getSurface()); // Step 2: Set sources mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//before setoutputFormat() mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//before setoutputFormat() mMediaRecorder.setoutputFormat(MediaRecorder.OutputFormat.MPEG_4); //设置视频输出的格式和编码 CamcorderProfile mProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P); // mMediaRecorder.setProfile(mProfile); mMediaRecorder.setVideoSize(640,480);//after setVideoSource(),after setoutFormat() mMediaRecorder.setAudioEncodingBitRate(44100); if (mProfile.videoBitRate > 2 * 1024 * 1024) mMediaRecorder.setVideoEncodingBitRate(2 * 1024 * 1024); else mMediaRecorder.setVideoEncodingBitRate(mProfile.videoBitRate); mMediaRecorder.setVideoFrameRate(mProfile.videoFrameRate);//after setVideoSource(),after setoutFormat() mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);//after setoutputFormat() mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//after setoutputFormat() //mMediaRecorder.setVideoEncodingBitRate(800); // Step 4: Set output file mMediaRecorder.setoutputFile(result.mediaPath); // Step 5: Set the preview output // mMediaRecorder.setorientationHint(90);//加了HTC的手机会有问题 Log.e("Yixia","OutputFile:" + result.mediaPath); mMediaRecorder.prepare(); mMediaRecorder.start(); mRecording = true; return result; } catch (IllegalStateException e) { e.printstacktrace(); Log.e("Yixia","startRecord",e); } catch (IOException e) { e.printstacktrace(); Log.e("Yixia",e); } catch (Exception e) { e.printstacktrace(); Log.e("Yixia",e); } } return null; } /** 停止录制 */ @Override public void stopRecord() { long endTime = System.currentTimeMillis(); if (mMediaRecorder != null) { //设置后不会崩 mMediaRecorder.setonErrorListener(null); mMediaRecorder.setPreviewdisplay(null); try { mMediaRecorder.stop(); } catch (IllegalStateException e) { Log.w("Yixia","stopRecord",e); } catch (RuntimeException e) { Log.w("Yixia",e); } catch (Exception e) { Log.w("Yixia",e); } } if (camera != null) { try { camera.lock(); } catch (RuntimeException e) { Log.e("Yixia",e); } } mRecording = false; } /** 释放资源 */ @Override public void release() { super.release(); if (mMediaRecorder != null) { mMediaRecorder.setonErrorListener(null); try { mMediaRecorder.release(); } catch (IllegalStateException e) { Log.w("Yixia",e); } } mMediaRecorder = null; } @Override public void onError(MediaRecorder mr,int what,int extra) { try { if (mr != null) mr.reset(); } catch (IllegalStateException e) { Log.w("Yixia",e); } catch (Exception e) { Log.w("Yixia",e); } if (mOnErrorListener != null) mOnErrorListener.onVideoError(what,extra); }
以上就是对Android MediaRecorder 资料整理,后续继续补充,有需要的朋友可以参考下。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。