【Android】录音问题之getMaxAmplitude()
这个方法是用来获取在前一次调用此方法之后录音中出现的最大振幅,文档解释如下:
Returns the maximum absolute amplitude that was sampled since the last call to this method. Call this only after the setAudioSource().
很多人遇到问题,说是返回值为0,文档中解释如下:the maximum absolute amplitude measured since the last call, or 0 when called for the first time。
所以这个方法是需要间隔一段时间调用一次的,也就是说,需要放在线程里面调用的。第一次调用会返回0。
最近需要使用这个方法获取音量的变化,对其返回值很好奇,查了一些资料(from stackoverflow),解释如下:
The MediaRecorder.getMaxAmplitude() function returns unsigned 16-bit integer values (0-32767). Which is probably just the abs() of the CD-quality sample values that range from -32768 to 32767. This means that they probably represent a 16-bit digitalization of the electrical output from 0-100% maximum voltage range of the microphone build into that mobile phone. Since even in one brand of mobile these microphones sometimes vary in their precise range not even to similar phones will necessarily return the same value given the same distance to the same sound source.
This value however correlates to sound pressure in Pascal since it's also a linear quantisation of the solund pressure, in the area where sound can be measured with the given microphone (which will not cover the entire sprectrum due to the limitations of the phone).
然后看了一下小米录音机的源码,它其中就有一段代码是用来反映录音音量大小的,关键代码如下:
1 int vuSize = MAX_VU_SIZE * mRecorder.getMaxAmplitude() / 32768;
其中MAX_VU_SIZE是小米将音量分的等级数,然后可以将获得的振幅处以32768(关于这个数字,前面一段资料里面有解释),这样子就能获得音量所处的等级。