时间:2021-05-19
具体表现:
调用MediaRecorder的start()与stop()间隔不能小于1秒(有时候大于1秒也崩),否则必崩。
错误信息:
java.lang.RuntimeException: stop failed. at android.media.MediaRecorder.stop(Native Method)解决办法:
在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", "startRecord", e); } catch (Exception e) { e.printStackTrace(); Log.e("Yixia", "startRecord", 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", "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; } /** 释放资源 */ @Override public 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; } @Override public 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); }以上就是对Android MediaRecorder 资料整理,后续继续补充,有需要的朋友可以参考下。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文为用MediaRecorder录制小视频的控件,可以设置视频录制的时间、空间大小、初始时是否打开摄像头等。此控件为组合控件,继承自LinearLayout,
我们玩玩手机的录像功能吧。做个DEMO。看看录制过程:复制代码代码如下:mediarecorder=newMediaRecorder();//创建mediare
通常来说,在进行Android项目开发的时候可以通过MediaRecorder和AudioRecord这两个工具来实现录音的功能,MediaRecorder直接
1、提供无线WIFI解决方案,支持WIFI直连。 2、支持视频录制、下载、回放、抓拍、一建分享等功能。 3、自动录制,实用且方便。 4、录制的视频自动
本文介绍了Android实现录音的实例代码(MediaRecorder),分享给大家,具体如下:Android提供了两个API用于录音的实现:MediaReco