解决Android MediaRecorder录制视频过短问题
程序员文章站
2024-03-04 20:57:30
具体表现:
调用mediarecorder的start()与stop()间隔不能小于1秒(有时候大于1秒也崩),否则必崩。
错误信息...
具体表现:
调用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 资料整理,后续继续补充,有需要的朋友可以参考下。
上一篇: i/o流
推荐阅读
-
解决Android MediaRecorder录制视频过短问题
-
汇总Android视频录制中常见问题
-
汇总Android视频录制中常见问题
-
Android使用MediaRecorder类进行录制视频
-
Android使用MediaRecorder类进行录制视频
-
解决Android WebView拦截url,视频播放加载失败的问题
-
android WebView 实现播放腾讯视频解决全屏播放问题
-
录制的视频在html5网页中用video标签无法播放的问题如何解决?
-
解决Android WebView拦截url,视频播放加载失败的问题
-
录制的视频在html5网页中用video标签无法播放的问题如何解决?