使用 Audio Toolbox 的 Audio Services 播放 AAC

使用 Audio Toolbox 的 Audio Services 播放 AAC

  • [使用 Audio Toolbox 的 Audio Services 播放 AAC](#使用 Audio Toolbox 的 Audio Services 播放 AAC)

使用 Audio Toolbox 的 Audio Services 播放 AAC

主要分为 3 步:

  1. 使用 AudioServicesCreateSystemSoundID 创建系统声音。
  2. 使用 AudioServicesAddSystemSoundCompletion 设置回调。
  3. 使用 AudioServicesPlaySystemSound 开始播放。

关于 AudioServicesPlaySystemSound 函数:

此功能播放短声音(持续时间为 30s 或更短)。由于声音可能会播放几秒钟,此功能是异步执行的。要了解声音何时完成播放,调用 AudioServicesAddSystemSoundCompletion 函数来注册回调函数。

在某些 iOS 设备上,可以通过 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate) 来调用振动。在其他 iOS 设备上,使用该常量调用此函数没有任何作用。

以下是该函数能播放的音频文件的限制:

  • No longer than 30 seconds in duration
  • In linear PCM or IMA4 (IMA/ADPCM) format
  • Packaged in a .caf, .aif, or .wav file

但实际上,AAC 文件也是能正常播放的,此外,该函数还有诸多限制:

  • 声音在当前系统音频音量下播放,没有编程音量控制。
  • 声音立即播放。
  • 不支持循环和立体声定位。
  • 无法同时播放,一次只能播放一个声音。
  • 声音在设备扬声器上本地播放,不使用音频路由。

核心代码:

objc 复制代码
- (void)btnClick:(UIButton *)sender
{
    self.mButton.hidden = YES;
    NSURL *audioUrl = [[NSBundle mainBundle] URLForResource:@"music" withExtension:@"aac"];
    SystemSoundID soundID;
    // create a system sound object
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)audioUrl, &soundID);
    // register a callback function that is invoked when a specified system sound finishes playing
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, &playCallback, (__bridge void *_Nullable)(self));
    // play a system sound object
    AudioServicesPlaySystemSound(soundID);
}

void playCallback(SystemSoundID ssID, void  *clientData)
{
    ViewController *vc = (__bridge ViewController *)clientData;
    vc->_mButton.hidden = NO;
}

项目地址:https://github.com/UestcXiye/AudioToolboxSystemSound。

相关推荐
Magnetic_h1 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
前端小超超1 天前
capacitor配置ios应用图标不同尺寸
ios·蓝桥杯·cocoa
2501_915106321 天前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
他们都不看好你,偏偏你最不争气1 天前
【iOS】AFNetworking
开发语言·macos·ios·objective-c
钟念1 天前
【打包app】uniapp打包ios端和安卓端app
macos·objective-c·cocoa
森之鸟2 天前
Mac电脑上如何打印出字体图标
前端·javascript·macos
m_136872 天前
Mac M 系列芯片 YOLOv8 部署教程(CPU/Metal 后端一键安装)
yolo·macos
小狮子安度因2 天前
AAC ADTS格式分析
网络·ffmpeg·aac
搜狐技术产品小编20232 天前
CAEmitterLayer:iOS 中创建炫酷粒子效果的魔法工具
macos·ios·objective-c·cocoa
未来之窗软件服务3 天前
操作系统应用开发(七)mac苹果模拟器——东方仙盟练气期
macos·仙盟创梦ide·东方仙盟·系统模拟器