這個方法是用來獲取在前一次調用此方法之后錄音中出現的最大振幅,文檔解釋如下:
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(關於這個數字,前面一段資料里面有解釋),這樣子就能獲得音量所處的等級。