iPhone 锁定 (Swift) 时声音不启动

Sound doesn't start when the iPhone is locked (Swift)

应用程序应在特定时间启动声音。要选择具体时间,我使用本地通知。到达特定时间并且应用程序位于前台时,声音会开始,即使我关闭应用程序(后台)或锁定 iPhone 声音也会继续播放。到目前为止,一切都很好。现在我想:具体时间到了,应用程序已经在后台或iPhone被锁定,它仍然应该启动声音。为此,我打开了 Background Modes,但没有任何改变。我在我的代码中添加了 mixWithOthers,仍然没有任何反应。还有其他建议吗?

import AVFoundation

var player: AVAudioPlayer?

func playSound() {
    guard let url = Bundle.main.url(forResource: "NameOfFile", withExtension: "mp3") else { return }

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: .default, options: AVAudioSession.CategoryOptions.mixWithOthers)
        try AVAudioSession.sharedInstance().setActive(true)

        player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)

        guard let player = player else { return }

        player.play()
        print("Start...")
    } catch let error {
        print(error.localizedDescription)
    }
}

后台模式:Screenshot

这个问题不是重复的。我正在寻找一种解决方案,当 iPhone 已 已经 locked/app 在声音之前的 背景 中时开始播放声音开始了。另一个问题是 continue start 之后的背景音频。

你不能做你想做的事。如果您的应用在后台运行超过 3 分钟,您的应用将被暂停并且不会获得任何 CPU 时间。

您可以安排本地通知。当它触发时,系统可以选择播放保存在您的应用程序包中的声音,或系统通知声音,然后点亮屏幕并显示一条通知,要求用户打开您的应用程序。在用户决定接受通知之前,您不会接到电话。