I am building a navigation App that should get audio notifications in some events. Currently, I am playing Audio by calling a Sound Manager I have written:
SoundManager.playSound(forSystemID: systemSoundIDs.newAlert.rawValue)
This is my SoundManager:
import Foundation
import AVFoundation
struct SoundManager {
static func playSound(forSystemID: SystemSoundID) {
// Play a sound
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(.playback, mode: .default, options: .mixWithOthers)
try audioSession.setActive(true, options: [])
AudioServicesPlayAlertSound(forSystemID)
} catch {
LogManager.logToFile("Error playing requested System Sound!")
}
}
}
This works, the sound and the haptic feedback play. However, it only plays when the device is unmuted, which is an issue for this use. I have tried many things but nothing seems to change the fact that it only plays when the device is unmuted. I am starting to think that it is impossible to play the sounds regardless of the device's mute state when using SystemSoundIDs. The issue is that I cannot use the URL because I don't know how to find it. I have gone trough hundreds of systems and have found ID's that fit what I was looking for, I do not know their names or the url...
TLDR: How do I play SystemSounds using AVAudioSession regardless of the devices mute state?
Systemsounds not playing using AudioServicesPlayAlertSound when the mute switch is turned on is probably by design.
If you want to play sounds, while the mute switch is on, you should use something like AVAudioPlayer (How to play a Sound) and set the category to the AVAudioSession to .playback (like you already did).
Keep in mind, when playing a sound with AVAudioPlayer, you need to provide a soundfile of your own, because the system sounds are not generally available (Mentioned here)