播放自定义声音
准备工作:
找到需要的声音(不要大于30秒),支持的格式有.mp3 .wav等;
将声音文件添加到系统中。
//播放声音需要的头文件 #import <AVFoundation/AVFoundation.h> #import <AudioToolbox/AudioToolbox.h> static SystemSoundID push = 0;
- (void)viewDidLoad { [super viewDidLoad]; [self playsound];
]; // Do any additional setup after loading the view. }
-(void)playsound{ //声音文件的路径 // NSString *path = [[NSBundle mainBundle] pathForResource:@"25" ofType:@"wav"];//添加的音频文件名字为(25.wav)
NSString *path = [[NSBundle mainBundle] pathForResource:@"25" ofType:@"mp3"];//添加的音频文件名字为(25.MP3)
if (path) { // AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&push);
AudioServicesPlaySystemSound(push); } AudioServicesPlaySystemSound(push); //播放注册的声音,(此句代码,可以在本类中的任意位置调用,不限于本方法中)
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //让手机震动
}