AudioServices
归属AudioToolbox
框架,AudioToolbox
是更底层的音频框架。
- 每个音频文件都有唯一的SystemSoundID
typedef UInt32 SystemSoundID;
- 震动,iPod无法震动
CF_ENUM(SystemSoundID)
{
kSystemSoundID_Vibrate = 0x00000FFF
};
- 创建SystemSoundID
extern OSStatus
AudioServicesCreateSystemSoundID( CFURLRef inFileURL,
SystemSoundID* outSystemSoundID)
__OSX_AVAILABLE_STARTING(__MAC_10_5,__IPHONE_2_0);
NSString *path = [[NSBundle mainBundle] pathForResource:@"videoChat_calling" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath: path];
SystemSoundID outSystemSoundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &outSystemSoundID);
注意:文件类型填写错误会crash。
- 注销SystemSoundID
extern OSStatus
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)
__OSX_AVAILABLE_STARTING(__MAC_10_5,__IPHONE_2_0);
- 播放带震动的音频
extern void
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)
__OSX_AVAILABLE_STARTING(__MAC_10_5,__IPHONE_2_0);
extern void
AudioServicesPlayAlertSoundWithCompletion( SystemSoundID inSystemSoundID,
void (^__nullable inCompletionBlock)(void))
__OSX_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_9_0);
- 播放音频
extern void
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
__OSX_AVAILABLE_STARTING(__MAC_10_5,__IPHONE_2_0);
extern void
AudioServicesPlaySystemSoundWithCompletion( SystemSoundID inSystemSoundID,
void (^__nullable inCompletionBlock)(void))
__OSX_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_9_0);
- 错误类型
CF_ENUM(OSStatus)
{
kAudioServicesNoError = 0,
kAudioServicesUnsupportedPropertyError = 'pty?',
kAudioServicesBadPropertySizeError = '!siz',
kAudioServicesBadSpecifierSizeError = '!spc',
kAudioServicesSystemSoundUnspecifiedError = -1500,
kAudioServicesSystemSoundClientTimedOutError = -1501,
kAudioServicesSystemSoundExceededMaximumDurationError = -1502
};
提升代码质量最神圣的三部曲:模块设计(谋定而后动) -->无错编码(知止而有得) -->开发自测(防患于未然)