[ad_1]
I am making an iOS sport with Swift and making an attempt to play system sounds when the iPhone is silenced to be able to hear sound results no matter quantity mode. The code to play the sound is:
AudioServicesPlaySystemSound(1000)
This works nice when the iPhone has the quantity rocker unmuted; nonetheless, if the telephone is silenced, nothing performs. If I modify this to:
AudioServicesPlayAlarmSound(1000)
I get vibrating on silent mode however not the audio. I’ve seen examples of find out how to setup the AppDelegate to alter the audio mode like the next:
class AppDelegate: NSObject, UIApplicationDelegate {
func utility(_ utility: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
do {
strive AVAudioSession.sharedInstance().setCategory(AVAudioSession.Class.playback, choices: AVAudioSession.CategoryOptions.mixWithOthers)
strive AVAudioSession.sharedInstance().setActive(true)
} catch {
#if DEBUG
print("ERROR: Can not play music in background: "(error)"")
#endif
}
return true
}
}
Nonetheless, I am utilizing iOS 15 and AppDelegate is not actually crucial anymore. What’s a contemporary means I can play system sounds whatever the quantity state in my sport? No different solutions on the positioning labored for me.
[ad_2]
