1、使用MediaPlayer播放
/**
* 播放拍照声音
*/
private void playShutterSound()
{
AudioManager am = (AudioManager)TakePicActivity.this.getSystemService(Context.AUDIO_SERVICE);
int volume = am.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
if (volume != 0)
{
MediaPlayer mp = MediaPlayer.create(TakePicActivity.this, Uri.parse("file:///system/media/audio/ui/camera_click.ogg"));
if (mp != null) {
mp.start();
}
}
}
2、使用SoundPool播放
rivate SoundPool mSoundPool = null;
private final int MAX_SOUND_POOL = 2; // 同时播放的音效个数
private final int SOUND_POOL_ALERT = 0;
private final int SOUND_POOL_TAKEPIC = 1;
private final int SOUND_POOL_FILESAVE = 2;
private boolean mSoundPoolLoadComplete = false;
private HashMap<Integer, Integer> mSoundPoolMap;
/**
*
*/
private void initSoundPool() {
// TODO Auto-generated method stub
mSoundPool = new SoundPool(MAX_SOUND_POOL, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mSoundPoolMap.put(SOUND_POOL_ALERT, mSoundPool.load(this, R.raw.mm, 1));
mSoundPoolMap.put(SOUND_POOL_TAKEPIC, mSoundPool.load(this, R.raw.takepic, 1));
mSoundPoolMap.put(SOUND_POOL_FILESAVE, mSoundPool.load(this, R.raw.takepic, 1));
mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
handler.sendEmptyMessage(SOUND_POOL_LOAD_COMPLETE);
}
});
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SOUND_POOL_LOAD_COMPLETE:
mSoundPoolLoadComplete = true;
break;
default:
break;
}
super.handleMessage(msg);
}
};
if(mSoundPoolLoadComplete){
mSoundPool.play(mSoundPoolMap.get(SOUND_POOL_ALERT), 1, 1, 0, 1, 1);//播放声音二次
mSoundPool.play(mSoundPoolMap.get(SOUND_POOL_ALERT), 1, 1, 0, 2, 1);//播放声音三次
}