iOS播放系統(tǒng)聲音,同時(shí)震動(dòng);播放自定義聲音

  • 播放&停止播放系統(tǒng)聲音代碼,播放一次,但不會(huì)循環(huán)播放
#import <AudioToolbox/AudioToolbox.h>
//  播放 以聲音編號(hào)1007舉例
AudioServicesPlaySystemSound(1007);
//  停止播放
AudioServicesRemoveSystemSoundCompletion(1007);
  • 震動(dòng)代碼
// 震動(dòng) 前提是你的iphone設(shè)置了允許震動(dòng)
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
// 停止震動(dòng)
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
  • 常見的幾個(gè)聲音
信息 
ReceivedMessage.caf--收到信息,僅在短信界面打開時(shí)播放。 
sms-received1.caf-------三全音 
sms-received2.caf-------管鐘琴 
sms-received3.caf-------玻璃 
sms-received4.caf-------圓號(hào) 
sms-received5.caf-------鈴聲 
sms-received6.caf-------電子樂(lè) 
SentMessage.caf--------發(fā)送信息 

郵件 
mail-sent.caf----發(fā)送郵件 
new-mail.caf-----收到新郵件 

電話 
dtmf-0.caf----------撥號(hào)面板0按鍵 
dtmf-1.caf----------撥號(hào)面板1按鍵 
dtmf-2.caf----------撥號(hào)面板2按鍵 
dtmf-3.caf----------撥號(hào)面板3按鍵 
dtmf-4.caf----------撥號(hào)面板4按鍵 
dtmf-5.caf----------撥號(hào)面板5按鍵 
dtmf-6.caf----------撥號(hào)面板6按鍵 
dtmf-7.caf----------撥號(hào)面板7按鍵 
dtmf-8.caf----------撥號(hào)面板8按鍵 
dtmf-9.caf----------撥號(hào)面板9按鍵 
dtmf-pound.caf---撥號(hào)面板#按鍵 
dtmf-star.caf------撥號(hào)面板*按鍵 
Voicemail.caf-----新語(yǔ)音郵件 

輸入設(shè)備聲音提示 
Tock.caf-----------------------點(diǎn)擊鍵盤 
begin_record.caf-----------開始錄音 
begin_video_record.caf--開始錄像 
photoShutter.caf------------快門聲 
end_record.caf--------------結(jié)束錄音 
end_video_record.caf-----結(jié)束錄像 
  • 具體的編號(hào) 請(qǐng)參照網(wǎng)址
http://iphonedevwiki.net/index.php/AudioServices
  • 播放自定義聲音

首先把你下載好的聲音文件拖入你的工程
下面貼出代碼


@interface Tools : NSObject
/**
 播放系統(tǒng)來(lái)電聲音
 @param name 文件名
 @param type 文件類型
 @param isAlert 是否伴隨震動(dòng)
 */
+ (SystemSoundID)playSystemSoundWithName:(NSString *)name type:(NSString *)type isAlert:(BOOL)isAlert;

// 停止播放來(lái)電聲音
+ (void)stopPlaySystemSound:(SystemSoundID)soundID;
@end
@implementation Tools
+ (SystemSoundID)playSystemSoundWithName:(NSString *)name type:(NSString *)type isAlert:(BOOL)isAlert {
    // 獲取文件路徑
    NSString *filePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
    // 加載音效文件,得到對(duì)應(yīng)的音效ID
    SystemSoundID soundID = 0;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)[NSURL fileURLWithPath:filePath], &soundID);
    // 播放音效
    if (isAlert) {
        AudioServicesPlayAlertSound(soundID);
    } else {
        AudioServicesPlaySystemSound(soundID);
    }
    return soundID;
}

+ (void)stopPlaySystemSound:(SystemSoundID)soundID {
    //把需要銷毀的音效文件的ID傳遞給它既可銷毀
    AudioServicesDisposeSystemSoundID(soundID);
}
@end
// 比如我這里的文件是voip_call.caf
[Tools playSystemSoundWithName:@"voip_call" type:@"caf" isAlert:YES];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容