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 |
|---|---|---|---|---|
| 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];
//利用打开的文件创建一个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)不能暂停音乐
因此,我们可以考虑综合两者的功能!
iOS系统声音服务(System Sound Services)的更多相关文章
- iOS系统声音列表
iOS系统声音列表 效果 说明 1. 点击cell就能发出声音 2. 只需要给出声音编号,就可以,非常简单易用 源码 https://github.com/YouXianMing/iOS-Utilit ...
- Swift - 系统声音服务的使用(播放声音,提醒,震动)
1,系统声音服务介绍: 系统声音服务提供了一个Api,用于播放不超过30秒的声音.它支持的文件格式有限,具体的说只有CAF.AIF和使用PCM或IMA/ADPCM数据的WAV文件. 但此函数没有提供操 ...
- 使用System Sound Services 播放音效(最简单,比较底层),调用AudioServicesPlaySystemSound()
1.适用范围:一些很小的提示或警告音频. 2.使用限制: 声音长度不能超过30秒 声音文件必须是PCM或IMA4(IMA/ADPCM)格式.(有时候可播放一些特殊的.mp3) 打包成.caf..aif ...
- iOS 之播放系统声音
导入框架: 代码: #import <UIKit/UIKit.h> #import <AudioToolbox/AudioToolbox.h> @interface MsgPl ...
- OSX/iOS 播放系统声音
方法1: 系统会自带了些声音,有时候一些操作用必要自己播放一下声音提醒一下,用bash的直接say something就ok了,写代码的时候呢?原来很简单的,一句: [[NSSound soundNa ...
- 检测iOS系统的定位服务
[CLLocationManager locationServicesEnabled]检测的是整个iOS系统的位置服务开关
- IOS调用系统声音(键盘声音)
#import <AudioToolbox/AudioToolbox.h> AudioServicesPlaySystemSound(1106); 注:括号中为系统声音的id,详见 htt ...
- ios开发——实用技术篇Swift篇&系统声音
系统声音 // MARK: - 系统声音 /*----- 系统声音 ------*/ @IBAction func systemSound() { //建立的SystemSoundID对象 var s ...
- iOS调用系统声音与振动
如何调用系统声音?[iphone 调用系统铃声与震动功能] 首先要在工程里加入Audio Toolbox framework这个库,然后在需要调用的文件里#import <AudioToolbo ...
随机推荐
- jQuery实现局部刷新页面数据绑定
今天遇到了一个问题:怎么样才能做到只刷新页面中的Repeater控件中的数据,在不用UploadPannel的情况下? 试了好多方法,无意间在看jquery文件时发现,使用load()方法即可解决此问 ...
- JavaScript事件onblur与onfocus区别
一.onblur 1.1 说明 onblur属性在元素失去焦点时触发,onblur常用于表单验证代码(例如用户离开表单字段). 1.2 示例 <input type="text&quo ...
- JavaWeb 之监听器
1. JavaWeb 监听器概述 在 JavaWeb 被监听的事件源为: ServletContext, HttpSession, ServletRequest, 即三大域对象. 监听域对象" ...
- 我的Android进阶之旅------>修改Android签名证书keystore的密码、别名alias以及别名密码
转载于:http://blog.k-res.net/archives/1229.html 和 http://blog.k-res.net/archives/1671.html ADT允许自定义调试用 ...
- Java语言实现简单FTP软件------>FTP协议分析(一)
FTP(File Transfer Protocol)就是文件传输协议.通过FTP客户端从远程FTP服务器上拷贝文件到本地计算机称为下载,将本地计算机上的文件复制到远程FTP服务器上称为上传,上传和下 ...
- grep和正则表达式参数
一:grep参数 1,-n :显示行号 2,-o :只显示匹配的内容 3,-q :静默模式,没有任何输出,得用$?来判断执行成功没有,即有没有过滤到想要的内容 4,-l :如果匹配成功,则只将 ...
- JavaScript Promise异步实现章节的下载显示
Links: JavaScript Promise:简介 1.一章一章顺序地下载显示下载显示 使用Array.reduce()和Promise.resolve()将各章的下载及显示作为整体串联起来. ...
- text_field text_tag 用法
= f.text_field :tax_category_id, :value => @invoice.tax_category.name, :class => "form-co ...
- 【转】Linux磁盘文件系统类型
1. 查询命令 [root@mail ~]# df -Th 参数说明: -T 显示文件系统类型. -h:以容易理解的格式输出文件系统大小,例如124KB.345MB.46GB. 转自:https:// ...
- Linux服务器iops性能测试-iozone
1. 选用工具: iozone 下载地址:http://www.iozone.org/ (直接下载rpm包) 2. 工具安装: 执行命令: rpm -ivh iozone-3-40 ...