原文:http://blog.csdn.net/wlm0813/article/details/51170574
系統(tǒng)聲音服務(System Sound Services)提供了一個接口,用于播放不超過30秒的聲音。它支持的文件格式有限,具體地說只有CAF、AIF和使用PCM或IMA/ADPCM數(shù)據(jù)的WAV文件。由于這些函數(shù)沒有提供操縱聲音和控制音量的功能,所以當你為多媒體或者游戲創(chuàng)建專門的配樂時,不要使用系統(tǒng)聲音服務。
iOS使用系統(tǒng)聲音服務來支持三種不同的通知:
1. 聲音:立刻播放一個簡單的聲音文件。如果手機被設置為靜音,用戶什么也聽不到
2. 提醒:播放一個聲音文件,如果手機被設置為靜音或震動,將通過震動提醒用戶
3. 震動:震動手機,而不考慮其他設置
要在項目中使用系統(tǒng)聲音服務,首先在自己的工程中添加AudioToolbox.framework庫,在.m文件中添加頭文件:要導入AudioToolbox.h
#import <AudioToolbox/AudioToolbox.h>
系統(tǒng)聲音服務并非是通過類實現(xiàn)的,而是使用傳統(tǒng)的C語言函數(shù)調用來觸發(fā)播放操作。要播放音頻,需要使用的兩個函數(shù)是AudioServicesCreateSystemSoundID 和 AudioServicesPlaySystemSound。還需要聲明一個類型為SystemSoundID的變量,它表示要使用的聲音文件。下面演示了如何加載并播放聲音:
然后在需要打開聲音的地方添加代碼:AudioServicesPlaySystemSound(audioID);
其中audioID就是你需要打開系統(tǒng)聲音的id號:附上系統(tǒng)對應提示音對照表:
There are some predefined system sounds, for the system sound ID in the range 1000 to 2000 (decimal), as shown below (from 2.0 to 5.0 beta). The system sounds are all stored in/System/Library/Audio/UISounds/.
Sound ID File name (iPhone) File name (iPod Touch) Category Note
1000 new-mail.caf new-mail.caf MailReceived
1001 mail-sent.caf mail-sent.caf MailSent
1002 Voicemail.caf Voicemail.caf VoicemailReceived
1003 ReceivedMessage.caf ReceivedMessage.caf SMSReceived
1004 SentMessage.caf SentMessage.caf SMSSent
1005 alarm.caf sq_alarm.caf CalendarAlert
1006 low_power.caf low_power.caf LowPower
1007 sms-received1.caf sms-received1.caf SMSReceived_Alert
1008 sms-received2.caf sms-received2.caf SMSReceived_Alert
1009 sms-received3.caf sms-received3.caf SMSReceived_Alert
1010 sms-received4.caf sms-received4.caf SMSReceived_Alert
1011 - - SMSReceived_Vibrate
1012 sms-received1.caf sms-received1.caf SMSReceived_Alert
1013 sms-received5.caf sms-received5.caf SMSReceived_Alert
1014 sms-received6.caf sms-received6.caf SMSReceived_Alert
1015 Voicemail.caf Voicemail.caf - Available since 2.1
1016 tweet_sent.caf tweet_sent.caf SMSSent Available since 5.0
1020 Anticipate.caf Anticipate.caf SMSReceived_Alert Available since 4.2
1021 Bloom.caf Bloom.caf SMSReceived_Alert Available since 4.2
1022 Calypso.caf Calypso.caf SMSReceived_Alert Available since 4.2
1023 Choo_Choo.caf Choo_Choo.caf SMSReceived_Alert Available since 4.2
1024 Descent.caf Descent.caf SMSReceived_Alert Available since 4.2
1025 Fanfare.caf Fanfare.caf SMSReceived_Alert Available since 4.2
1026 Ladder.caf Ladder.caf SMSReceived_Alert Available since 4.2
1027 Minuet.caf Minuet.caf SMSReceived_Alert Available since 4.2
1028 News_Flash.caf News_Flash.caf SMSReceived_Alert Available since 4.2
1029 Noir.caf Noir.caf SMSReceived_Alert Available since 4.2
1030 Sherwood_Forest.caf Sherwood_Forest.caf SMSReceived_Alert Available since 4.2
1031 Spell.caf Spell.caf SMSReceived_Alert Available since 4.2
1032 Suspense.caf Suspense.caf SMSReceived_Alert Available since 4.2
1033 Telegraph.caf Telegraph.caf SMSReceived_Alert Available since 4.2
1034 Tiptoes.caf Tiptoes.caf SMSReceived_Alert Available since 4.2
1035 Typewriters.caf Typewriters.caf SMSReceived_Alert Available since 4.2
1036 Update.caf Update.caf SMSReceived_Alert Available since 4.2
1050 ussd.caf ussd.caf USSDAlert
1051 SIMToolkitCallDropped.caf SIMToolkitCallDropped.caf SIMToolkitTone
1052 SIMToolkitGeneralBeep.caf SIMToolkitGeneralBeep.caf SIMToolkitTone
1053 SIMToolkitNegativeACK.caf SIMToolkitNegativeACK.caf SIMToolkitTone
1054 SIMToolkitPositiveACK.caf SIMToolkitPositiveACK.caf SIMToolkitTone
1055 SIMToolkitSMS.caf SIMToolkitSMS.caf SIMToolkitTone
1057 Tink.caf Tink.caf PINKeyPressed
1070 ct-busy.caf ct-busy.caf AudioToneBusy There was no category for this sound before 4.0.
1071 ct-congestion.caf ct-congestion.caf AudioToneCongestion There was no category for this sound before 4.0.
1072 ct-path-ack.caf ct-path-ack.caf AudioTonePathAcknowledge There was no category for this sound before 4.0.
1073 ct-error.caf ct-error.caf AudioToneError There was no category for this sound before 4.0.
1074 ct-call-waiting.caf ct-call-waiting.caf AudioToneCallWaiting There was no category for this sound before 4.0.
1075 ct-keytone2.caf ct-keytone2.caf AudioToneKey2 There was no category for this sound before 4.0.
1100 lock.caf sq_lock.caf ScreenLocked
1101 unlock.caf sq_lock.caf ScreenUnlocked
1102 - - FailedUnlock
1103 Tink.caf sq_tock.caf KeyPressed
1104 Tock.caf sq_tock.caf KeyPressed
1105 Tock.caf sq_tock.caf KeyPressed
1106 beep-beep.caf sq_beep-beep.caf ConnectedToPower
1107 RingerChanged.caf RingerChanged.caf RingerSwitchIndication
1108 photoShutter.caf photoShutter.caf CameraShutter
1109 shake.caf shake.caf ShakeToShuffle Available since 3.0
1110 jbl_begin.caf jbl_begin.caf JBL_Begin Available since 3.0
1111 jbl_confirm.caf jbl_confirm.caf JBL_Confirm Available since 3.0
1112 jbl_cancel.caf jbl_cancel.caf JBL_Cancel Available since 3.0
1113 begin_record.caf begin_record.caf BeginRecording Available since 3.0
1114 end_record.caf end_record.caf EndRecording Available since 3.0
1115 jbl_ambiguous.caf jbl_ambiguous.caf JBL_Ambiguous Available since 3.0
1116 jbl_no_match.caf jbl_no_match.caf JBL_NoMatch Available since 3.0
1117 begin_video_record.caf begin_video_record.caf BeginVideoRecording Available since 3.0
1118 end_video_record.caf end_video_record.caf EndVideoRecording Available since 3.0
1150 vc~invitation-accepted.caf vc~invitation-accepted.caf VCInvitationAccepted Available since 4.0
1151 vc~ringing.caf vc~ringing.caf VCRinging Available since 4.0
1152 vc~ended.caf vc~ended.caf VCEnded Available since 4.0
1153 ct-call-waiting.caf ct-call-waiting.caf VCCallWaiting Available since 4.1
1154 vc~ringing.caf vc~ringing.caf VCCallUpgrade Available since 4.1
1200 dtmf-0.caf dtmf-0.caf TouchTone
1201 dtmf-1.caf dtmf-1.caf TouchTone
1202 dtmf-2.caf dtmf-2.caf TouchTone
1203 dtmf-3.caf dtmf-3.caf TouchTone
1204 dtmf-4.caf dtmf-4.caf TouchTone
1205 dtmf-5.caf dtmf-5.caf TouchTone
1206 dtmf-6.caf dtmf-6.caf TouchTone
1207 dtmf-7.caf dtmf-7.caf TouchTone
1208 dtmf-8.caf dtmf-8.caf TouchTone
1209 dtmf-9.caf dtmf-9.caf TouchTone
1210 dtmf-star.caf dtmf-star.caf TouchTone
1211 dtmf-pound.caf dtmf-pound.caf TouchTone
1254 long_low_short_high.caf long_low_short_high.caf Headset_StartCall
1255 short_double_high.caf short_double_high.caf Headset_Redial
1256 short_low_high.caf short_low_high.caf Headset_AnswerCall
1257 short_double_low.caf short_double_low.caf Headset_EndCall
1258 short_double_low.caf short_double_low.caf Headset_CallWaitingActions
1259 middle_9_short_double_low.caf middle_9_short_double_low.caf Headset_TransitionEnd
1300 Voicemail.caf Voicemail.caf SystemSoundPreview
1301 ReceivedMessage.caf ReceivedMessage.caf SystemSoundPreview
1302 new-mail.caf new-mail.caf SystemSoundPreview
1303 mail-sent.caf mail-sent.caf SystemSoundPreview
1304 alarm.caf sq_alarm.caf SystemSoundPreview
1305 lock.caf sq_lock.caf SystemSoundPreview
1306 Tock.caf sq_tock.caf KeyPressClickPreview The category was SystemSoundPreview before 3.2.
1307 sms-received1.caf sms-received1.caf SMSReceived_Selection
1308 sms-received2.caf sms-received2.caf SMSReceived_Selection
1309 sms-received3.caf sms-received3.caf SMSReceived_Selection
1310 sms-received4.caf sms-received4.caf SMSReceived_Selection
1311 - - SMSReceived_Vibrate
1312 sms-received1.caf sms-received1.caf SMSReceived_Selection
1313 sms-received5.caf sms-received5.caf SMSReceived_Selection
1314 sms-received6.caf sms-received6.caf SMSReceived_Selection
1315 Voicemail.caf Voicemail.caf SystemSoundPreview Available since 2.1
1320 Anticipate.caf Anticipate.caf SMSReceived_Selection Available since 4.2
1321 Bloom.caf Bloom.caf SMSReceived_Selection Available since 4.2
1322 Calypso.caf Calypso.caf SMSReceived_Selection Available since 4.2
1323 Choo_Choo.caf Choo_Choo.caf SMSReceived_Selection Available since 4.2
1324 Descent.caf Descent.caf SMSReceived_Selection Available since 4.2
1325 Fanfare.caf Fanfare.caf SMSReceived_Selection Available since 4.2
1326 Ladder.caf Ladder.caf SMSReceived_Selection Available since 4.2
1327 Minuet.caf Minuet.caf SMSReceived_Selection Available since 4.2
1328 News_Flash.caf News_Flash.caf SMSReceived_Selection Available since 4.2
1329 Noir.caf Noir.caf SMSReceived_Selection Available since 4.2
1330 Sherwood_Forest.caf Sherwood_Forest.caf SMSReceived_Selection Available since 4.2
1331 Spell.caf Spell.caf SMSReceived_Selection Available since 4.2
1332 Suspense.caf Suspense.caf SMSReceived_Selection Available since 4.2
1333 Telegraph.caf Telegraph.caf SMSReceived_Selection Available since 4.2
1334 Tiptoes.caf Tiptoes.caf SMSReceived_Selection Available since 4.2
1335 Typewriters.caf Typewriters.caf SMSReceived_Selection Available since 4.2
1336 Update.caf Update.caf SMSReceived_Selection Available since 4.2
1350 - - RingerVibeChanged
1351 - - SilentVibeChanged
4095 - - Vibrate There was no category for this sound before 2.2.
In the SDK this is the constant kSystemSoundID_Vibrate.
當然你可以定義自己的提示音,打開自己工程中的音頻文件,代碼如下:
static SystemSoundID _ringSystemSoundID;
static void ringAudioServicesSystemSoundCompletionProc(SystemSoundID ssID, void *clientData)
{
AudioServicesPlayAlertSound(ssID);
}
void ringStartRing()
{
// 獲取文件所在的路徑
NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/jad0007a.wav"];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient
error:nil];
//利用打開的文件創(chuàng)建一個soundID
AudioServicesCreateSystemSoundID((__bridge CFURLRef) path, &_ringSystemSoundID);
//當聲音播放完后的回調函數(shù),完成后將調用ringAudioServicesSystemSoundCompletionProc函數(shù)進行循環(huán)播放
AudioServicesAddSystemSoundCompletion(_ringSystemSoundID, NULL, NULL, ringAudioServicesSystemSoundCompletionProc, NULL);
//通過創(chuàng)建的soundID打開對應的音頻文件
AudioServicesPlayAlertSound(_ringSystemSoundID);
}
通過調用AudioServicesAddSystemSoundCompletion 添加可選的系統(tǒng)聲音完成回調,通知你的程序聲音已經(jīng)播放完畢。如果你不使用一個接一個的短聲音,那么一般可以跳過該步驟。
void ringStopRing()
{
if (_ringSystemSoundID != 0) {
//移除系統(tǒng)播放完成后的回調函數(shù)
AudioServicesRemoveSystemSoundCompletion(_ringSystemSoundID);
//銷毀創(chuàng)建的SoundID
AudioServicesDisposeSystemSoundID(_ringSystemSoundID);
_ringSystemSoundID = 0;
}
}
調用 AudioServicesDisposeSystemSoundID 以及相應的聲音可以清除你的聲音,該操作可以釋放聲音對象以及相關的所有資源
對于提醒音來說,與系統(tǒng)聲音之間的差別在于,如果手機處于靜音狀態(tài),提醒音將自動觸發(fā)震動。提醒音的設置和用法與系統(tǒng)聲音相同,區(qū)別在于系統(tǒng)聲音調用AudioServicesPlaySystemSound,而提醒音調用AudioServicesPlayAlertSound。
震動更容易實現(xiàn),只需要調用AudioServicesPlaySystemSound,并將常量kSystemSoundID_Vibrate傳遞給它:
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
如果視圖震動不支持震動的設備(如iPad2),將靜默失敗。因此將震動代碼留在應用程序中不會有任何害處,而不管目標設備是什么。
////////////
AVAudioPlayer和SystemSoundService。這兩種功能在一定程度上都適合作為游戲音效的播放方式。而兩者也各有其優(yōu)缺點。下面我們來總結一下有哪些:
1)AVAudioPlayer的優(yōu)點
(1)可以播放任意長度音樂;
(2)可以循環(huán)播放;
(3)可以控制播放的時間;
(4)可以控制聲道的音量實現(xiàn)立體聲效果;
(5)可以調整音量。
2)AVAudioPlayer的缺點
(1)播放之前必須先加載,不能立即播放;
(2)一次一個AudioPlayer只能播放所加載的音樂,不能同時播放好幾次同樣的音樂。
(3)沒有振動效果
3)SystemSoundService的優(yōu)點:
(1)能夠立即播放
(2)能夠在同一時間播放多次音樂
(3)可以加上振動效果
4)SystemSoundService的缺點:
(1)不能循環(huán)播放
(2)不能控制播放時間
(3)不能控制聲道
(4)不能控制音量
(5)不能暫停音樂
因此,我們可以考慮綜合兩者的功能!
iOS系統(tǒng)聲音ID
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
相關閱讀更多精彩內容
- 點擊查看http://jingyan.baidu.com/article/ed2a5d1f656ba509f6be...
- asset需要用到的框架:PhotoKit 需導入:#import OC實現(xiàn)代碼: PHFetchOptions ...
- 原文鏈接 最近開發(fā)App時,需要用到聲音服務。在查閱資料和文檔后,發(fā)現(xiàn)iOS上播放聲音有好幾種方法。準備針對每種方...
- SystemSoundIDsound; NSString*path = [NSStringstringWithFo...