开发者

kAudioSessionCategory_MediaPlayback is silenced with ringer off?

开发者 https://www.devze.com 2023-03-14 03:08 出处:网络
according to the documentation the 开发者_如何学Gocorrect way to allow sounds to be played even if the ringer switch is set to off is like so:

according to the documentation the 开发者_如何学Gocorrect way to allow sounds to be played even if the ringer switch is set to off is like so:

 UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;

    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);

I then throughout the app play short wav files that play perfectly using

AudioServicesPlaySystemSound(sysSound)

....until the ringer is switched off and then.....silence?

No matter what I do I cannot get the sound to play if the ringer is off?

Any one ..... please?


Did you try using AudioSessionSetActive(YES); at the end?


AudioServicesPlaySystemSound never plays while the ringer is switched off in my experience. If you play sounds using AVAudioPlayer instead, you should get the behavior you are looking for:

let player: AVAudioPlayer = try AVAudioPlayer(contentsOf: url)
player.delegate = self
player.play()

You will also need to activate the audio session before playing if you have not already. And set an audio session category that supports sounds while silenced like you already did. Consider stopping the audio session when no audio is playing to conserve battery life.

A notable difference between AudioServices and AVAudioPlayer is that if the ringer is silenced, AudioServices playback will return almost immediately while AVAudioPlayer will take the full length of the sound before completing. As far as I know, the only way to detect and respond to the user's ringer setting is to use AudioServices instead of AVAudioPlayer. If you just want to play while silenced though this should not be relevant.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号