设置启用的时候加上 mAudioManager.loadSoundEffects(); 关闭按键音的时候加上mAudioManager.unloadSoundEffects(); 应该就可以了。
提供下参考 有这么几个类AudioManager SoundPool。
我提供一段我们项目中的代码,各作用其余的你自己相关搜索下
/**
* 播放音效
*/
@TargetApi(8)
public static void PlaySoundEffect(final SeekerActivity activity, int wavid){
final SoundPool sp = new SoundPool(20, AudioManager.STREAM_MUSIC, 100);
sp.load(activity, wavid ,1);
sp.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
AudioManager am = (AudioManager)activity.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent/streamVolumeMax;
sp.play(1, volume, volume, 0, 0, 1);
}
}
wavid为你对应的音效资源id,play()方法调用时设置音量大小。
方法有很多,不限于这一个。