iOS - 系统提醒短音频

Demo下载地址

iPhone端系统Audio资源路径:/System/Library/Audio/UISounds

首先,通过 NSFileManager 获取资源路径下的所有路径(文件夹/文件)

    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSURL * _soundDataURL = [NSURL URLWithString:@"/System/Library/Audio/UISounds"];
    NSDirectoryEnumerator * _soundDirectoryEnum = [fileManager enumeratorAtURL:_soundDataURL
                                                    includingPropertiesForKeys:@[NSURLIsDirectoryKey]
                                                                       options:0
                                                                  errorHandler:^BOOL(NSURL * _Nonnull url, NSError * _Nonnull error) {
                                                                      return YES;
                                                                  }];

接着遍历枚举,然后获取系统音频的SoundID:

/*!
    @typedef        SystemSoundID
    @abstract       SystemSoundIDs are created by the System Sound client application
                    for playback of a provided AudioFile.
*/
typedef UInt32      SystemSoundID;

通过使用 AudioServicesCreateSystemSoundID 方法:

/*!
    @function       AudioServicesCreateSystemSoundID
    @abstract       Allows the application to designate an audio file for playback by the System Sound server.
    @discussion     SystemSoundIDs are used passed to
                    AudioServicesPlayAlertSound() & AudioServicesPlaySystemSound(),
                    designating the audio data to be played by the System Sound server.
    @param          inFileURL
                        A CFURLRef for an AudioFile.
    @param          outSystemSoundID
                        Returns a SystemSoundID.
*/
extern OSStatus 
AudioServicesCreateSystemSoundID(   CFURLRef                    inFileURL,
                                    SystemSoundID*              outSystemSoundID)

获取SoundID后,最后,使用 AudioServicesPlaySystemSound 播放这个SoundID 音频:
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

posted @   lvable  阅读(351)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析
点击右上角即可分享
微信分享提示