Android录音时获取分贝值的方法代码实例
程序员文章站
2022-06-23 10:23:55
参考文章android中实时获取音量分贝值详解:
public class mediarecorderdemo {
private final strin...
参考文章android中实时获取音量分贝值详解:
public class mediarecorderdemo { private final string tag = "mediarecord"; private mediarecorder mmediarecorder; public static final int max_length = 1000 * 60 * 10;// 最大录音时长1000*60*10; private string filepath; public mediarecorderdemo(){ this.filepath = "/dev/null"; } public mediarecorderdemo(file file) { this.filepath = file.getabsolutepath(); } private long starttime; private long endtime; /** * 开始录音 使用amr格式 * * 录音文件 * @return */ public void startrecord() { // 开始录音 /* ①initial:实例化mediarecorder对象 */ if (mmediarecorder == null) mmediarecorder = new mediarecorder(); try { /* ②setaudiosource/setvediosource */ mmediarecorder.setaudiosource(mediarecorder.audiosource.mic);// 设置麦克风 /* ②设置音频文件的编码:aac/amr_nb/amr_mb/default 声音的(波形)的采样 */ mmediarecorder.setoutputformat(mediarecorder.outputformat.default); /* * ②设置输出文件的格式:three_gpp/mpeg-4/raw_amr/default three_gpp(3gp格式 * ,h263视频/arm音频编码)、mpeg-4、raw_amr(只支持音频且音频编码要求为amr_nb) */ mmediarecorder.setaudioencoder(mediarecorder.audioencoder.amr_nb); /* ③准备 */ mmediarecorder.setoutputfile(filepath); mmediarecorder.setmaxduration(max_length); mmediarecorder.prepare(); /* ④开始 */ mmediarecorder.start(); // audiorecord audiorecord. /* 获取开始时间* */ starttime = system.currenttimemillis(); updatemicstatus(); log.i("action_start", "starttime" + starttime); } catch (illegalstateexception e) { log.i(tag, "call startamr(file mrecaudiofile) failed!" + e.getmessage()); } catch (ioexception e) { log.i(tag, "call startamr(file mrecaudiofile) failed!" + e.getmessage()); } } /** * 停止录音 * */ public long stoprecord() { if (mmediarecorder == null) return 0l; endtime = system.currenttimemillis(); log.i("action_end", "endtime" + endtime); mmediarecorder.stop(); mmediarecorder.reset(); mmediarecorder.release(); mmediarecorder = null; log.i("action_length", "time" + (endtime - starttime)); return endtime - starttime; } private final handler mhandler = new handler(); private runnable mupdatemicstatustimer = new runnable() { public void run() { updatemicstatus(); } }; /** * 更新话筒状态 * */ private int base = 1; private int space = 100;// 间隔取样时间 private void updatemicstatus() { if (mmediarecorder != null) { double ratio = (double)mmediarecorder.getmaxamplitude() /base; double db = 0;// 分贝 if (ratio > 1) db = 20 * math.log10(ratio); log.d(tag,"分贝值:"+db); mhandler.postdelayed(mupdatemicstatustimer, space); } } }
上一篇: ps图片白色背景怎么去掉? ps合成去背景只留文字的技巧
下一篇: C语言if选择结构语句详解