iOS播放提示音

在iOS开发中难免会遇到需要播放提示音的地方
注意#import

//播放test.wav文件
static SystemSoundID soundIDTest = 0;//当soundIDTest == kSystemSoundID_Vibrate的时候为震动
NSString * path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];
if (path) {
     AudioServicesCreateSystemSoundID( (__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundIDTest );
}
AudioServicesPlaySystemSound( soundIDTest );```

//播放系统提示音
SystemSoundID soundIDTest = 1007;
AudioServicesPlaySystemSound(sound);

NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",soundName,soundType];
//[[NSBundle bundleWithIdentifier:@"com.apple.UIKit" ]pathForResource:soundName ofType:soundType];//得到苹果框架资源UIKit.framework ,从中取出所要播放的系统声音的路径
//[[NSBundle mainBundle] URLForResource: @"tap" withExtension: @"aif"]; 获取自定义的声音
if (path) {
OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&sound);
if (error != kAudioServicesNoError) {//获取的声音的时候,出现错误
sound = nil;
}
}


![](http://upload-images.jianshu.io/upload_images/701353-1b164f4a4ec56781.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/701353-72a2bf1315743ddb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/701353-0bfe311c5bc58f90.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/701353-df7ff7c0c832fc2d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/701353-d140d97bd4e60c5b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


欢迎关注我的[微博](http://weibo.com/u/2209572342/home?topnav=1&wvr=6&mod=logo)和[博客](http://dwz.cn/iOS_BK)。

你可能感兴趣的:(iOS播放提示音)