iOS系统声音服务(System Sound Services)

系统声音服务(System Sound Services)提供了一个接口,用于播放不超过30秒的声音。它支持的文件格式有限,具体地说只有CAF、AIF和使用PCM或IMA/ADPCM数据的WAV文件。由于这些函数没有提供操纵声音和控制音量的功能,所以当你为多媒体或者游戏创建专门的配乐时,不要使用系统声音服务。

iOS使用系统声音服务来支持三种不同的通知:
1. 声音:立刻播放一个简单的声音文件。如果手机被设置为静音,用户什么也听不到
2. 提醒:播放一个声音文件,如果手机被设置为静音或震动,将通过震动提醒用户
3. 震动:震动手机,而不考虑其他设置

要在项目中使用系统声音服务,首先在自己的工程中添加AudioToolbox.framework库,在.m文件中添加头文件:要导入AudioToolbox.h

#import <AudioToolbox/AudioToolbox.h>

系统声音服务并非是通过类实现的,而是使用传统的C语言函数调用来触发播放操作。要播放音频,需要使用的两个函数是AudioServicesCreateSystemSoundID 和 AudioServicesPlaySystemSound。还需要声明一个类型为SystemSoundID的变量,它表示要使用的声音文件。下面演示了如何加载并播放声音:


然后在需要打开声音的地方添加代码:AudioServicesPlaySystemSound(audioID);

其中audioID就是你需要打开系统声音的id号:附上系统对应提示音对照表:

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
1000new-mail.cafnew-mail.cafMailReceived 
1001mail-sent.cafmail-sent.cafMailSent 
1002Voicemail.cafVoicemail.cafVoicemailReceived 
1003ReceivedMessage.cafReceivedMessage.cafSMSReceived 
1004SentMessage.cafSentMessage.cafSMSSent 
1005alarm.cafsq_alarm.cafCalendarAlert 
1006low_power.caflow_power.cafLowPower 
1007sms-received1.cafsms-received1.cafSMSReceived_Alert 
1008sms-received2.cafsms-received2.cafSMSReceived_Alert 
1009sms-received3.cafsms-received3.cafSMSReceived_Alert 
1010sms-received4.cafsms-received4.cafSMSReceived_Alert 
1011--SMSReceived_Vibrate 
1012sms-received1.cafsms-received1.cafSMSReceived_Alert 
1013sms-received5.cafsms-received5.cafSMSReceived_Alert 
1014sms-received6.cafsms-received6.cafSMSReceived_Alert 
1015Voicemail.cafVoicemail.caf-Available since 2.1
1016tweet_sent.caftweet_sent.cafSMSSentAvailable since 5.0
1020Anticipate.cafAnticipate.cafSMSReceived_AlertAvailable since 4.2
1021Bloom.cafBloom.cafSMSReceived_AlertAvailable since 4.2
1022Calypso.cafCalypso.cafSMSReceived_AlertAvailable since 4.2
1023Choo_Choo.cafChoo_Choo.cafSMSReceived_AlertAvailable since 4.2
1024Descent.cafDescent.cafSMSReceived_AlertAvailable since 4.2
1025Fanfare.cafFanfare.cafSMSReceived_AlertAvailable since 4.2
1026Ladder.cafLadder.cafSMSReceived_AlertAvailable since 4.2
1027Minuet.cafMinuet.cafSMSReceived_AlertAvailable since 4.2
1028News_Flash.cafNews_Flash.cafSMSReceived_AlertAvailable since 4.2
1029Noir.cafNoir.cafSMSReceived_AlertAvailable since 4.2
1030Sherwood_Forest.cafSherwood_Forest.cafSMSReceived_AlertAvailable since 4.2
1031Spell.cafSpell.cafSMSReceived_AlertAvailable since 4.2
1032Suspense.cafSuspense.cafSMSReceived_AlertAvailable since 4.2
1033Telegraph.cafTelegraph.cafSMSReceived_AlertAvailable since 4.2
1034Tiptoes.cafTiptoes.cafSMSReceived_AlertAvailable since 4.2
1035Typewriters.cafTypewriters.cafSMSReceived_AlertAvailable since 4.2
1036Update.cafUpdate.cafSMSReceived_AlertAvailable since 4.2
1050ussd.cafussd.cafUSSDAlert 
1051SIMToolkitCallDropped.cafSIMToolkitCallDropped.cafSIMToolkitTone 
1052SIMToolkitGeneralBeep.cafSIMToolkitGeneralBeep.cafSIMToolkitTone 
1053SIMToolkitNegativeACK.cafSIMToolkitNegativeACK.cafSIMToolkitTone 
1054SIMToolkitPositiveACK.cafSIMToolkitPositiveACK.cafSIMToolkitTone 
1055SIMToolkitSMS.cafSIMToolkitSMS.cafSIMToolkitTone 
1057Tink.cafTink.cafPINKeyPressed 
1070ct-busy.cafct-busy.cafAudioToneBusyThere was no category for this sound before 4.0.
1071ct-congestion.cafct-congestion.cafAudioToneCongestionThere was no category for this sound before 4.0.
1072ct-path-ack.cafct-path-ack.cafAudioTonePathAcknowledgeThere was no category for this sound before 4.0.
1073ct-error.cafct-error.cafAudioToneErrorThere was no category for this sound before 4.0.
1074ct-call-waiting.cafct-call-waiting.cafAudioToneCallWaitingThere was no category for this sound before 4.0.
1075ct-keytone2.cafct-keytone2.cafAudioToneKey2There was no category for this sound before 4.0.
1100lock.cafsq_lock.cafScreenLocked 
1101unlock.cafsq_lock.cafScreenUnlocked 
1102--FailedUnlock 
1103Tink.cafsq_tock.cafKeyPressed 
1104Tock.cafsq_tock.cafKeyPressed 
1105Tock.cafsq_tock.cafKeyPressed 
1106beep-beep.cafsq_beep-beep.cafConnectedToPower 
1107RingerChanged.cafRingerChanged.cafRingerSwitchIndication 
1108photoShutter.cafphotoShutter.cafCameraShutter 
1109shake.cafshake.cafShakeToShuffleAvailable since 3.0
1110jbl_begin.cafjbl_begin.cafJBL_BeginAvailable since 3.0
1111jbl_confirm.cafjbl_confirm.cafJBL_ConfirmAvailable since 3.0
1112jbl_cancel.cafjbl_cancel.cafJBL_CancelAvailable since 3.0
1113begin_record.cafbegin_record.cafBeginRecordingAvailable since 3.0
1114end_record.cafend_record.cafEndRecordingAvailable since 3.0
1115jbl_ambiguous.cafjbl_ambiguous.cafJBL_AmbiguousAvailable since 3.0
1116jbl_no_match.cafjbl_no_match.cafJBL_NoMatchAvailable since 3.0
1117begin_video_record.cafbegin_video_record.cafBeginVideoRecordingAvailable since 3.0
1118end_video_record.cafend_video_record.cafEndVideoRecordingAvailable since 3.0
1150vc~invitation-accepted.cafvc~invitation-accepted.cafVCInvitationAcceptedAvailable since 4.0
1151vc~ringing.cafvc~ringing.cafVCRingingAvailable since 4.0
1152vc~ended.cafvc~ended.cafVCEndedAvailable since 4.0
1153ct-call-waiting.cafct-call-waiting.cafVCCallWaitingAvailable since 4.1
1154vc~ringing.cafvc~ringing.cafVCCallUpgradeAvailable since 4.1
1200dtmf-0.cafdtmf-0.cafTouchTone 
1201dtmf-1.cafdtmf-1.cafTouchTone 
1202dtmf-2.cafdtmf-2.cafTouchTone 
1203dtmf-3.cafdtmf-3.cafTouchTone 
1204dtmf-4.cafdtmf-4.cafTouchTone 
1205dtmf-5.cafdtmf-5.cafTouchTone 
1206dtmf-6.cafdtmf-6.cafTouchTone 
1207dtmf-7.cafdtmf-7.cafTouchTone 
1208dtmf-8.cafdtmf-8.cafTouchTone 
1209dtmf-9.cafdtmf-9.cafTouchTone 
1210dtmf-star.cafdtmf-star.cafTouchTone 
1211dtmf-pound.cafdtmf-pound.cafTouchTone 
1254long_low_short_high.caflong_low_short_high.cafHeadset_StartCall 
1255short_double_high.cafshort_double_high.cafHeadset_Redial 
1256short_low_high.cafshort_low_high.cafHeadset_AnswerCall 
1257short_double_low.cafshort_double_low.cafHeadset_EndCall 
1258short_double_low.cafshort_double_low.cafHeadset_CallWaitingActions 
1259middle_9_short_double_low.cafmiddle_9_short_double_low.cafHeadset_TransitionEnd 
1300Voicemail.cafVoicemail.cafSystemSoundPreview 
1301ReceivedMessage.cafReceivedMessage.cafSystemSoundPreview 
1302new-mail.cafnew-mail.cafSystemSoundPreview 
1303mail-sent.cafmail-sent.cafSystemSoundPreview 
1304alarm.cafsq_alarm.cafSystemSoundPreview 
1305lock.cafsq_lock.cafSystemSoundPreview 
1306Tock.cafsq_tock.cafKeyPressClickPreviewThe category was SystemSoundPreview before 3.2.
1307sms-received1.cafsms-received1.cafSMSReceived_Selection 
1308sms-received2.cafsms-received2.cafSMSReceived_Selection 
1309sms-received3.cafsms-received3.cafSMSReceived_Selection 
1310sms-received4.cafsms-received4.cafSMSReceived_Selection 
1311--SMSReceived_Vibrate 
1312sms-received1.cafsms-received1.cafSMSReceived_Selection 
1313sms-received5.cafsms-received5.cafSMSReceived_Selection 
1314sms-received6.cafsms-received6.cafSMSReceived_Selection 
1315Voicemail.cafVoicemail.cafSystemSoundPreviewAvailable since 2.1
1320Anticipate.cafAnticipate.cafSMSReceived_SelectionAvailable since 4.2
1321Bloom.cafBloom.cafSMSReceived_SelectionAvailable since 4.2
1322Calypso.cafCalypso.cafSMSReceived_SelectionAvailable since 4.2
1323Choo_Choo.cafChoo_Choo.cafSMSReceived_SelectionAvailable since 4.2
1324Descent.cafDescent.cafSMSReceived_SelectionAvailable since 4.2
1325Fanfare.cafFanfare.cafSMSReceived_SelectionAvailable since 4.2
1326Ladder.cafLadder.cafSMSReceived_SelectionAvailable since 4.2
1327Minuet.cafMinuet.cafSMSReceived_SelectionAvailable since 4.2
1328News_Flash.cafNews_Flash.cafSMSReceived_SelectionAvailable since 4.2
1329Noir.cafNoir.cafSMSReceived_SelectionAvailable since 4.2
1330Sherwood_Forest.cafSherwood_Forest.cafSMSReceived_SelectionAvailable since 4.2
1331Spell.cafSpell.cafSMSReceived_SelectionAvailable since 4.2
1332Suspense.cafSuspense.cafSMSReceived_SelectionAvailable since 4.2
1333Telegraph.cafTelegraph.cafSMSReceived_SelectionAvailable since 4.2
1334Tiptoes.cafTiptoes.cafSMSReceived_SelectionAvailable since 4.2
1335Typewriters.cafTypewriters.cafSMSReceived_SelectionAvailable since 4.2
1336Update.cafUpdate.cafSMSReceived_SelectionAvailable since 4.2
1350--RingerVibeChanged 
1351--SilentVibeChanged 
4095--VibrateThere 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];

    //利用打开的文件创建一个soundID

    AudioServicesCreateSystemSoundID((__bridge CFURLRef) path, &_ringSystemSoundID);

    //当声音播放完后的回调函数,完成后将调用ringAudioServicesSystemSoundCompletionProc函数进行循环播放

    AudioServicesAddSystemSoundCompletion(_ringSystemSoundID, NULL, NULL, ringAudioServicesSystemSoundCompletionProc, NULL);

    //通过创建的soundID打开对应的音频文件

    AudioServicesPlayAlertSound(_ringSystemSoundID);

}

通过调用AudioServicesAddSystemSoundCompletion 添加可选的系统声音完成回调,通知你的程序声音已经播放完毕。如果你不使用一个接一个的短声音,那么一般可以跳过该步骤。


void ringStopRing()

{

    if (_ringSystemSoundID != 0) {

        //移除系统播放完成后的回调函数

        AudioServicesRemoveSystemSoundCompletion(_ringSystemSoundID);

        //销毁创建的SoundID

        AudioServicesDisposeSystemSoundID(_ringSystemSoundID);

        _ringSystemSoundID = 0;

    }

}

调用 AudioServicesDisposeSystemSoundID 以及相应的声音可以清除你的声音,该操作可以释放声音对象以及相关的所有资源


对于提醒音来说,与系统声音之间的差别在于,如果手机处于静音状态,提醒音将自动触发震动。提醒音的设置和用法与系统声音相同,区别在于系统声音调用AudioServicesPlaySystemSound而提醒音调用AudioServicesPlayAlertSound

震动更容易实现,只需要调用AudioServicesPlaySystemSound,并将常量kSystemSoundID_Vibrate传递给它:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

如果视图震动不支持震动的设备(如iPad2),将静默失败。因此将震动代码留在应用程序中不会有任何害处,而不管目标设备是什么。


AVAudioPlayer和SystemSoundService。这两种功能在一定程度上都适合作为游戏音效的播放方式。而两者也各有其优缺点。下面我们来总结一下有哪些:

1)AVAudioPlayer的优点

(1)可以播放任意长度音乐;

(2)可以循环播放;

(3)可以控制播放的时间;

(4)可以控制声道的音量实现立体声效果;

(5)可以调整音量。

2)AVAudioPlayer的缺点

(1)播放之前必须先加载,不能立即播放;

(2)一次一个AudioPlayer只能播放所加载的音乐,不能同时播放好几次同样的音乐。

(3)没有振动效果

3)SystemSoundService的优点:

(1)能够立即播放

(2)能够在同一时间播放多次音乐

(3)可以加上振动效果

4)SystemSoundService的缺点:

(1)不能循环播放

(2)不能控制播放时间

(3)不能控制声道

(4)不能控制音量

(5)不能暂停音乐

因此,我们可以考虑综合两者的功能!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值