目前做的一個項目里用到了提示音,但是又不想添加提示音到庫里,便開始研究調用系統自帶的提示音,最后終于找到了。
開始在CC上查發現好像很多人都在問,但沒人回答,我就把自己查到的東西和寫的一個demo給大家分享下吧
首先要在工程里加入Audio Toolbox framework這個庫,然后在需要調用的文件里#import <AudioToolbox/AudioToolbox.h>
最后在需要播放提示音的地方寫上
AudioServicesPlaySystemSound(1106);
注:括號中為系統聲音的id,詳見最下面的列表。
為了方便大家測試系統聲音,我寫了一個demo供大家使用下載。
另外,如果想用自己的音頻文件創建系統聲音來播放的同學可以參考如下代碼。
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/jad0007a.wav"];
//declare a system sound
id SystemSoundID soundID;
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
//Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
//Use audio services to play the sound
AudioServicesPlaySystemSound(soundID);
順便附上系統提示音對應的列表
AudioServices