当前位置:
首页 >
Android 用MediaRecorder录制视频太短崩的问题
发布时间:2024/4/15
66
豆豆
生活随笔
收集整理的这篇文章主要介绍了
Android 用MediaRecorder录制视频太短崩的问题
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
具体表现:
调用MediaRecorder的start()与stop()间隔不能小于1秒(有时候大于1秒也崩),否则必崩。
错误信息:
java.lang.RuntimeException: stop failed.at android.media.MediaRecorder.stop(Native Method)解决办法:
在stop以前调用setOnErrorListener(null);就行了!
相关代码:
/** 开始录制 */@Overridepublic 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 sourcesmMediaRecorder.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);elsemMediaRecorder.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", "startRecord", e);} catch (Exception e) {e.printStackTrace();Log.e("Yixia", "startRecord", e);}}return null;}/** 停止录制 */@Overridepublic 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", "stopRecord", e);} catch (Exception e) {Log.w("Yixia", "stopRecord", e);}}if (camera != null) {try {camera.lock();} catch (RuntimeException e) {Log.e("Yixia", "stopRecord", e);}}mRecording = false;}/** 释放资源 */@Overridepublic void release() {super.release();if (mMediaRecorder != null) {mMediaRecorder.setOnErrorListener(null);try {mMediaRecorder.release();} catch (IllegalStateException e) {Log.w("Yixia", "stopRecord", e);} catch (Exception e) {Log.w("Yixia", "stopRecord", e);}}mMediaRecorder = null;}@Overridepublic void onError(MediaRecorder mr, int what, int extra) {try {if (mr != null)mr.reset();} catch (IllegalStateException e) {Log.w("Yixia", "stopRecord", e);} catch (Exception e) {Log.w("Yixia", "stopRecord", e);}if (mOnErrorListener != null)mOnErrorListener.onVideoError(what, extra);}
转载于:https://www.cnblogs.com/zhujiabin/p/5973334.html
超强干货来袭 云风专访:近40年码龄,通宵达旦的技术人生总结
以上是生活随笔为你收集整理的Android 用MediaRecorder录制视频太短崩的问题的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: DTRACE简介(2)
- 下一篇: Android开发之动态添加控件