Android 錄音獲取分貝值的辦法


參考:http://blog.csdn.net/greatpresident/article/details/38402147

 

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);  
        }  
    }  
}  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM