在需要聲音的類的.h文件中添加 #import <AudioToolbox/AudioToolbox.h>
static SystemSoundID shake_sound_male_id = 0;
-(void) playSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"shake_sound_male" ofType:@"wav"];
if (path) {
//注冊聲音到系統
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&shake_sound_male_id);
AudioServicesPlaySystemSound(shake_sound_male_id);
// AudioServicesPlaySystemSound(shake_sound_male_id);//如果無法再下面播放,可以嘗試在此播放
}
AudioServicesPlaySystemSound(shake_sound_male_id); //播放注冊的聲音,(此句代碼,可以在本類中的任意位置調用,不限於本方法中)
// AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //讓手機震動
}