開發(fā)過程中需要用到蘋果自帶的系統(tǒng)提示音疆液,下面總結(jié)了一下關(guān)于系統(tǒng)提示音播放的方法
第一步首先得導(dǎo)入AudioToolbox框架
#import <AudioToolbox/AudioToolbox.h>
播放系統(tǒng)自帶的提示聲
播放系統(tǒng)自帶的提示聲很簡(jiǎn)單榛鼎,只需要兩行代碼就能搞定了:
//定義一個(gè)SystemSoundID
SystemSoundID soundID = 1007;//具體參數(shù)詳情下面貼出來
//播放聲音
AudioServicesPlaySystemSound(soundID);
關(guān)于SystemSoundID的相關(guān)參數(shù)介紹和系統(tǒng)所有的鈴聲的介紹
播放自定義的提示聲,既有聲音也帶振動(dòng)
- (void)playNotifySound {
//獲取路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"];
//定義一個(gè)SystemSoundID
SystemSoundID soundID;
//判斷路徑是否存在
if (path) {
//創(chuàng)建一個(gè)音頻文件的播放系統(tǒng)聲音服務(wù)器
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:path]), &soundID);
//判斷是否有錯(cuò)誤
if (error != kAudioServicesNoError) {
NSLog(@"%d",(int)error);
}
}
//播放聲音和振動(dòng)
AudioServicesPlayAlertSoundWithCompletion(soundID, ^{
//播放成功回調(diào)
});
}
只有振動(dòng)沒有聲音
//手機(jī)只振動(dòng)沒聲音
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
只有聲音不帶振動(dòng)
//必須得是自定義的聲音家凯,經(jīng)過測(cè)試系統(tǒng)的聲音好像都帶振動(dòng)
- (void)playNotifySound {
//獲取路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"candoNotifySound" ofType:@"mp3"];
//定義一個(gè)帶振動(dòng)的SystemSoundID
SystemSoundID soundID = 1000;
//判斷路徑是否存在
if (path) {
//創(chuàng)建一個(gè)音頻文件的播放系統(tǒng)聲音服務(wù)器
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)([NSURL fileURLWithPath:path]), &soundID);
//判斷是否有錯(cuò)誤
if (error != kAudioServicesNoError) {
NSLog(@"%d",(int)error);
}
}
//只播放聲音,沒振動(dòng)
AudioServicesPlaySystemSound(soundID);
}