527 //---------------------------------------------------------
528 // Audio data supply
529 //--------------------
530 /**
531 * Reads audio data from the audio hardware for recording into a buffer.
532 * @param audioData the array to which the recorded audio data is written.
533 * @param offsetInBytes index in audioData from which the data is written expressed in bytes.
534 * @param sizeInBytes the number of requested bytes.
535 * @return the number of bytes that were read or or {@link #ERROR_INVALID_OPERATION}
536 * if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
537 * the parameters don't resolve to valid data and indexes.
538 * The number of bytes will not exceed sizeInBytes.
539 */
540 public int read(byte[] audioData, int offsetInBytes, int sizeInBytes) {
541 if (mState != STATE_INITIALIZED) {
542 return ERROR_INVALID_OPERATION;
543 }
544
545 if ( (audioData == null) || (offsetInBytes < 0 ) || (sizeInBytes < 0)
546 || (offsetInBytes + sizeInBytes > audioData.length)) {
547 return ERROR_BAD_VALUE;
548 }
549
550 return native_read_in_byte_array(audioData, offsetInBytes, sizeInBytes);
551 }
这是read 的实现函数,你用这个函数得到音量不对吧? buffer 里面是记录的record的数据呀。
用这个函数吧: 442 void AudioTrack::getVolume(float* left, float* right)
443 {
444 *left = mVolume[LEFT];
445 *right = mVolume[RIGHT];
446 }
528 // Audio data supply
529 //--------------------
530 /**
531 * Reads audio data from the audio hardware for recording into a buffer.
532 * @param audioData the array to which the recorded audio data is written.
533 * @param offsetInBytes index in audioData from which the data is written expressed in bytes.
534 * @param sizeInBytes the number of requested bytes.
535 * @return the number of bytes that were read or or {@link #ERROR_INVALID_OPERATION}
536 * if the object wasn't properly initialized, or {@link #ERROR_BAD_VALUE} if
537 * the parameters don't resolve to valid data and indexes.
538 * The number of bytes will not exceed sizeInBytes.
539 */
540 public int read(byte[] audioData, int offsetInBytes, int sizeInBytes) {
541 if (mState != STATE_INITIALIZED) {
542 return ERROR_INVALID_OPERATION;
543 }
544
545 if ( (audioData == null) || (offsetInBytes < 0 ) || (sizeInBytes < 0)
546 || (offsetInBytes + sizeInBytes > audioData.length)) {
547 return ERROR_BAD_VALUE;
548 }
549
550 return native_read_in_byte_array(audioData, offsetInBytes, sizeInBytes);
551 }
这是read 的实现函数,你用这个函数得到音量不对吧? buffer 里面是记录的record的数据呀。
用这个函数吧: 442 void AudioTrack::getVolume(float* left, float* right)
443 {
444 *left = mVolume[LEFT];
445 *right = mVolume[RIGHT];
446 }
解决方案 »
- Android中的SearchView,如何去掉其后面的那个X按钮?
- 怎么样可以固定我的Linearlayout的宽度?
- 帮忙myeclipse开发android出现打不开xml文件的问题
- 分享一个android开发者超级交流群(500人)
- 安卓开发入门问题。。入门好难啊
- 用BaseAdapter显示ListView的问题
- 请教,android 4.x如果要把鼠标显示在光标层,应该如何做?
- Android 电子书的应用,大段的文字存放在哪比较好啊?
- 关于二维码
- 想要实现一个3级联动的筛选功能,有偿
- 本人大专在校生,想要参加一个小比赛,关于 java ,android 都可以,各位大侠帮忙给点有创意的点子呗
- 有谁曾用ant把src打成一个jar包吗
我没记错的话read中获取到的buffer 记录到的是音波振幅,这个和音量是成正比的。
请问getVolume获取的是怎么音量?怎么用的?
for (int i = 0; i < buffer.length; i++) {
v += buffer[i] * buffer[i];
}
int value = (int) (Math.abs((int)(v /(float)r)/10000) >> 1);我计算了噪音
value 的 值 控制 为 0 到 100 之间 0为最小 》= 100为最大!!
哇 牛牛牛
我是个算法白痴,拿笔划了半天,亮点是float r
就是说这个算法的实质就是把 变化 的值给缩到最小 从而控制在value 的0 到100可控
int t = 0;
for (int i = 0; i < buffer.length; i++) {
t += Math.abs(buffer[i]);
}
t /= buffer.length;
System.out.println("你的音量" + t);
我的博客:http://blog.csdn.net/xys289187120
不过开眼了,上层也有问题..学习,先MARK!
我觉得问题好像不是出在差值上。我通过反复测试发现五楼的算法好像并不是把值控制在0到100之间,我对着手机麦克播放音乐,首先把音乐声音关到最小,然后逐步放大音量。麦克接收到的音量值也会跟这重零开始逐步增长。但当接收到的音量达到50多的时候便停止增长了。而且无论我将音乐声音再放大录制的音量也是50多。如果将五楼公式中是“>> 1”去掉则最高停留在110多。所以我认为五楼的算法应该是在0到60之间。
但现在的问题是如果我手机一边播放音乐一边录音,播放音量开到一半的时候录音音量就达到顶峰了。这是很让人郁闷的一件事。
http://topic.csdn.net/u/20101229/14/01099efc-d8e1-43d5-a714-2a84f72c4496.html