編輯:關於Android編程
參考文章Android中實時獲取音量分貝值詳解:http://www.jb51.net/article/64806.htm
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); } } }
喜歡另辟蹊徑的我,在這裡廢話不多說了,直接上代碼和圖片了。效果圖如下:第一步:MainActivity的代碼如下:package net.loonggg.test; i
.java代碼如下: package org.lxh.demo; import android.app.Activity; import android
大家好!首先申明一下,本人是一名初級Android程序員,剛接觸EventBus(3.0的用法)有什麼說的不對的地方還請大神指點一二。此文章是面向能力不強,對於大神級別就
寫在開頭:已經研究生畢業快一年半了,一直在一家游戲公司做客戶端研發。至於這篇文章講的卻是服務端的東西,主要是因為以前一直沒想寫博客,學到的東西也一直記在本子上就得了。本人