Play sound on iOS 9 in silent mode

老子叫甜甜 提交于 2019-12-20 19:37:05

问题


In iOS 9 (Xcode 7, Swift 2.0) I'm trying to play a sound in silent mode using the following code:

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

According to other answers and Apple's documentation I thought this should work but it doesn't play a sound on iOS 9 when in silent mode. It does play it when not in silent mode. From Apple's doc:

AVAudioSessionCategoryPlayback -- Playback only. Plays audio even with the screen locked and with the Ring/Silent switch set to silent. Use this category for an app whose audio playback is of primary importance.

Am I missing something here or is there a other/new way to get this to work?


回答1:


This is my code:

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

AudioPlayer.play()

It works on my iPhone.

Good luck!




回答2:


For Swift 3.2:

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


来源:https://stackoverflow.com/questions/32683188/play-sound-on-ios-9-in-silent-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!