当前位置: 首页 > 工具软件 > AudioRecorder > 使用案例 >

android获取录音音量大小,关于AudioRecorder录音获取音量的问题

周作人
2023-12-01

while (isPrepared) {

int bytes = mRecorder.read(buffer, 0, bufferSize);

if (bytes > 0) {

ringBuffer.write(buffer, bytes);

}

//                        //取得音量

long v = 0;

// 将 buffer 内容取出,进行平方和运算

for (int i = 0; i 

v += buffer[i] * buffer[i];

}

float f = (int) (Math.abs((int)(v /(float)bytes)/10000) >> 1);

logger.i("AudioManager : f = " +f);

//                        // 平方和除以数据总长度,得到音量大小。

//                        double mean = v / (double) bytes;

//                        double volume = 10 * Math.log10(mean);

//                        logger.i("AudioManager : 总长度:" + mean);

//                        logger.i("AudioManager : 分贝值:" + volume);

}

本人的代码是上面那样子的,获取音量时,使用float f = (int) (Math.abs((int)(v /(float)bytes)/10000) >> 1);的话,获取到的值永远为0,使用注释掉的方法的话,分贝值一直等于34.xxxxxx,本人的代码哪里出问题了?(方法都是网上找的)

解决方案

10

权限写没写

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

10

(int) (Math.abs((int)(v /(float)bytes)/10000) >> 1);  把前面的int强转去掉看下结果

10

引用:

Quote: 引用:

(int) (Math.abs((int)(v /(float)bytes)/10000) >> 1);  把前面的int强转去掉看下结果

其实本人主要用的是第二种方法,第一种方法本人觉得不太靠谱

你不是在问为啥这里获取的分贝有问题吗

10

值问题,你就找值呗,你看v和bytes的值有没有问题呀,然后再看整个计算表达式的算法有没有问题。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于AudioRecorder录音获取音量的问题!

 类似资料: