'SwiftUI AudioServicesPlayAlertSoundWithCompletion sometimes does not play a sound

I built a small convenience struct to let an APP play "Beep" sounds in certain circumstances like this:

struct Beeper {
  private var prefs = UserPreferences()
  
  
  static func beepWithVibration(systemSoundID: SystemSoundID = 1057 ){
   do {
     try AVAudioSession.sharedInstance().setCategory(.playAndRecord)
     try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)       
     try AVAudioSession.sharedInstance().setActive(true)
   } catch {}
   AudioServicesPlayAlertSoundWithCompletion(systemSoundID) {}
  }
}

It basically works, but sometimes (quite rarely) no sound is played.
Do you have an idea how this can be done more reliably?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source