1.
2.MediaRecorder mMediaRecorder;
3.int ratio = mMediaRecorder.getMaxAmplitude() / BASE;
4.int db = 0;// 分贝 也可以理解为定义的音量大小
5.if (ratio > 1)
6.db = (int) (20 * Math.log10(ratio));//db就是我们需要取得的音量的值。
7.//(int) (20 * Math.log10(ratio))振幅和音量大小的公式
8.//BASE的值由自己测试获得,我是怎么获取这个值得呢?
9.//开启麦克风,不对麦克风说话,而由周围噪声获取的值 大概在300到600之间 我取得是600这个基准值。
一下是一部分应用的代码:
1.package com.taobao.baby.manager;
2.
3.import java.io.File;
4.import java.io.IOException;
5.
6.import com.taobao.baby.R;
7.import com.taobao.baby.util.Log;
8.import android.media.MediaRecorder;
9.import android.os.Handler;
10.import android.widget.ImageView;
11.
12./**
13. * amr音频处理
14. *
15. * @author hongfa.yy
16. * @version 创建时间2012-11-21 下午4:33:28
17. */
18.public class RecordManager {
19. private final String TAG = "RecordManager";
20. private MediaRecorder mMediaRecorder;
21. public static final int MAX_LENGTH = 1000 * 60 * 10;// 最大录音时长1000*60*10;
22. private File file;
23.
24. public RecordManager(File file,ImageView view) {
25. this.file = file;
26. this.view=view;
27. }
28. public RecordManager(File file) {
29. this.file = file;
30. }
31.
32. private long startTime;
33. private long endTime;
34.
35. /**
36. * 开始录音 使用amr格式
37. *
38. * @param mRecAudioFile
39. * 录音文件
40. * @return
41. */
42. public void startRecord() {
43. // 开始录音
44. /* ①Initial:实例化MediaRecorder对象 */
45. if (mMediaRecorder == null)
46. mMediaRecorder = new MediaRecorder();
47. try {
48. /* ②setAudioSource/setVedioSource */
49. mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置麦克风
50. /*
51. * ②设置输出文件的格式:THREE_GPP/MPEG-4/RAW_AMR/Default THREE_GPP(3gp格式
52. * ,H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB)
53. */
54. mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
55. /* ②设置音频文件的编码:AAC/AMR_NB/AMR_MB/Default 声音的(波形)的采样 */
56. mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
57. /* ③准备 */
58. mMediaRecorder.setOutputFile(file.getAbsolutePath());
59. mMediaRecorder.setMaxDuration(MAX_LENGTH);
60. mMediaRecorder.prepare();
61. /* ④开始 */
62. mMediaRecorder.start();
63. // AudioRecord audioRecord.
64. /* 获取开始时间* */
65. startTime = System.currentTimeMillis();
66. // pre=mMediaRecorder.getMaxAmplitude();
67. updateMicStatus();
68. Log.i("ACTION_START", "startTime" + startTime);
69. } catch (IllegalStateException e) {
70. Log.i(TAG,
71. "call startAmr(File mRecAudioFile) failed!"
72. + e.getMessage());
73. } catch (IOException e) {
74. Log.i(TAG,
75. "call startAmr(File mRecAudioFile) failed!"
76. + e.getMessage());
77. }
78.
79. }
80.
81. /**
82. * 停止录音
83. *
84. * @param mMediaRecorder
85. */
86. public long stopRecord() {
87. if (mMediaRecorder == null)
88. return 0L;
89. endTime = System.currentTimeMillis();
90. Log.i("ACTION_END", "endTime" + endTime);
91. mMediaRecorder.stop();
92. mMediaRecorder.reset();
93. mMediaRecorder.release();
94. mMediaRecorder = null;
95. Log.i("ACTION_LENGTH", "Time" + (endTime - startTime));
96. return endTime - startTime;
97. }
98.
99. private final Handler mHandler = new Handler();
100. private Runnable mUpdateMicStatusTimer = new Runnable() {
101. public void run() {
102. updateMicStatus();
103. }
104. };
105.
106. /**
107. * 更新话筒状态 分贝是也就是相对响度 分贝的计算公式K=20lg(Vo/Vi) Vo当前振幅值 Vi基准值为600:我是怎么制定基准值的呢? 当20
108. * * Math.log10(mMediaRecorder.getMaxAmplitude() / Vi)==0的时候vi就是我所需要的基准值
109. * 当我不对着麦克风说任何话的时候,测试获得的mMediaRecorder.getMaxAmplitude()值即为基准值。
110. * Log.i("mic_", "麦克风的基准值:" + mMediaRecorder.getMaxAmplitude());前提时不对麦克风说任何话
111. */
112. private int BASE = 600;
113. private int SPACE = 300;// 间隔取样时间
114. private ImageView view;
115.
116. private void updateMicStatus() {
117. if (mMediaRecorder != null && view != null) {
118. // int vuSize = 10 * mMediaRecorder.getMaxAmplitude() / 32768;
119. int ratio = mMediaRecorder.getMaxAmplitude() / BASE;
120. int db = 0;// 分贝
121. if (ratio > 1)
122. db = (int) (20 * Math.log10(ratio));
123. switch (db / 4) {
124. case 0:
125. view.setImageBitmap(null);
126. break;
127. case 1:
128. view.setImageResource(R.drawable.audio_recorder_volume_1);
129. break;
130. case 2:
131. view.setImageResource(R.drawable.audio_recorder_volume_2);
132. break;
133. case 3:
134. view.setImageResource(R.drawable.audio_recorder_volume_3);
135. break;
136. case 4:
137. view.setImageResource(R.drawable.audio_recorder_volume_4);
138. break;
139. case 5:
140. view.setImageResource(R.drawable.audio_recorder_volume_5);
141. break;
142. default:
143. view.setImageResource(R.drawable.audio_recorder_volume_5);
144. break;
145. }
146. mHandler.postDelayed(mUpdateMicStatusTimer, SPACE);
147. /*
148. * if (db > 1) { vuSize = (int) (20 * Math.log10(db)); Log.i("mic_",
149. * "麦克风的音量的大小:" + vuSize); } else Log.i("mic_", "麦克风的音量的大小:" + 0);
150. */
151. }
152. }
153.
154.}