使用 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。

相关推荐
Digitally3 小时前
如何在iPhone 17/16/15上显示电池百分比
ios·cocoa·iphone
私人珍藏库9 小时前
Clip Studio Paint EX v2.0.6 For MacOS – 官方版本+逆向补丁下载,M4芯片Mac实机测试好用
macos
绝世唐门三哥9 小时前
MacOS-Terminal直接command解压缩文件文件夹
macos·命令行·压缩
2501_9160088910 小时前
App 上架需要什么?从开发者账号到开心上架(Appuploader)免 Mac 上传的完整流程指南
macos·ios·小程序·uni-app·objective-c·cocoa·iphone
crazy_yun10 小时前
laya3如何打包mac包
macos·layabox3
麦麦大数据13 小时前
MacOS 安装Python 3.13【同时保留旧版本】
开发语言·python·macos·python安装
开开心心就好21 小时前
电子报纸离线保存:一键下载多报PDF工具
网络·笔记·macos·pdf·word·音视频·phpstorm
淡忘_cx1 天前
Dify 插件开发与打包教程 (Mac)
macos
2501_915918411 天前
App 上架苹果商店全流程详解 从开发者账号申请到开心上架(Appuploader)跨平台免 Mac 上传实战指南
macos·ios·小程序·uni-app·objective-c·cocoa·iphone
亚林瓜子1 天前
SpringBoot中使用tess4j进行OCR(在macos上面开发)
java·spring boot·macos·ocr·lstm·tess4j