在 iOS 13 中播放系统声音

Mik*_*sya 2 avfoundation ios swift ios13

我曾经能够使用下面的代码播放系统音频文件。这在 iOS 13 中对于新旧应用程序均不起作用。

import AudioToolbox
import AVFoundation

...

    func tock() {
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category(rawValue: convertFromAVAudioSessionCategory(AVAudioSession.Category.ambient)))

            var myAlertSound: SystemSoundID = 0
            let url: URL = URL(string: "/System/Library/Audio/UISounds/Tock.caf")!
            AudioServicesCreateSystemSoundID( (url) as CFURL, &myAlertSound)
            AudioServicesPlaySystemSound(myAlertSound)
        } catch {
            print("Error in audio feedback")
        }
    }

    // Helper function inserted by Swift 4.2 migrator.
    private func convertFromAVAudioSessionCategory(_ input: AVAudioSession.Category) -> String {
        return input.rawValue
    }
Run Code Online (Sandbox Code Playgroud)

我也没有看到“音频反馈错误”打印。

有没有办法在 iOS 13 中播放简单的声音?

Mik*_*sya 6

这有效:

AudioServicesPlaySystemSound(SystemSoundID(1104))
Run Code Online (Sandbox Code Playgroud)

所有系统声音的列表可在此处找到:https://github.com/TUNER88/iOSSystemSoundsLibrary