在iOS 9(Xcode 7,Swift 2.0)中,我尝试使用以下代码以静音模式播放声音:
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .MixWithOthers)
try! AVAudioSession.sharedInstance().setActive(true)
AudioServicesPlayAlertSound(1005)
Run Code Online (Sandbox Code Playgroud)
根据其他答案和Apple的文档,我认为这应该可行,但在静音模式下它在iOS 9上没有播放声音.它不是在静音模式下播放它.来自Apple的文档:
AVAudioSessionCategoryPlayback - 仅播放.即使屏幕锁定并且Ring/Silent开关设置为静音,也会播放音频.将此类别用于其音频播放最重要的应用.
我在这里遗漏了什么,或者是否有其他/新方法让这个工作?
这是我的代码:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
}
catch {
// report for an error
}
AudioPlayer.play()
Run Code Online (Sandbox Code Playgroud)
它适用于我的iPhone.
祝好运!