Android 4.4增加了LoudnessEnhancer类,该类的注释为:
.LoudnessEnhancer is an audio effect for increasing audio loudness. The processing is parametrized by a target gain value, which determines the maximum amount by which an audio signal will be amplified; signals amplified outside of the sample range supported by the platform are compressed. An application creates a LoudnessEnhancer object to instantiate and control a this audio effect in the audio framework. To attach the LoudnessEnhancer to a particular AudioTrack or MediaPlayer, specify the audio session ID of this AudioTrack or MediaPlayer when constructing the effect (see AudioTrack.getAudioSessionId() and MediaPlayer.getAudioSessionId())
具体调用方法为:
//声明LoudnessEnhancer类
private LoudnessEnhancer loudnessEnhancer;
// 定义播放声音的MediaPlayer
private MediaPlayer mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//初始化 MediaPlayer
mPlayer = MediaPlayer.create(this,Uri.fromFile(file));
//初始化loudnessEnhancer
loudnessEnhancer = new LoudnessEnhancer(mPlayer.getAudioSessionId());
//设置用于音频效果的目标增益,该数值过大会造成失真,根据设备自己选择数值
loudnessEnhancer.setTargetGain(4000);
//启用声音增强
loudnessEnhancer.setEnabled(true);
mPlayer.start();
}
@Override
public void onDestroy() {
if(loudnessEnhancer != null){
loudnessEnhancer.release();
mPlayer.release();
mPlayer = null;
}
super.onDestroy();
}