在iOS 9上以静音模式播放声音

在iOS 9(Xcode 7,Swift 2.0)中,我尝试使用以下代码以静音模式播放声音:

try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .MixWithOthers) try! AVAudioSession.sharedInstance().setActive(true) AudioServicesPlayAlertSound(1005) 

根据其他答案和Apple的文档,我认为这应该可行,但在静音模式下,它在iOS 9上不会发出声音。 它不是在静音模式下播放它。 来自Apple的文档:

AVAudioSessionCategoryPlayback – 仅播放。 即使屏幕锁定并且Ring / Silent开关设置为静音,也会播放音频。 将此类别用于其音频播放最重要的应用。

我在这里遗漏了什么,还是有其他/新的方式让这个工作?

这是我的代码:

 do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers) } catch { // report for an error } AudioPlayer.play() 

它适用于我的iPhone。

祝你好运!

对于Swift 3.2:

 do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.duckOthers) } catch { // report for an error }