Other audio being ducked even when device is silenced and category is ambient

222 Views Asked by At

I would like the audio in my app to duck music being played in the background, but also not be played when the ringer/silent switch is switched to silent mode. I am able to get this behavior with the following AVAudioSession configuration, with one caveat -- when music is playing and the device is silenced, it is still ducking the music, even though my app (correctly) doesn't actually play any audio.

Is there a configuration to make this work properly? It seems strange that the system would duck others when the app doesn't actually play audio. Is it possible this is a bug in iOS?

try AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default, options: [.duckOthers, .interruptSpokenAudioAndMixWithOthers])
1

There are 1 best solutions below

1
On

This is what I have implemented and I just muted my iPad and it plays no sound. But I am not sure if the behaviour is same on iPhone as I generally always have my phone on mute and still hear audio...

  do {
    try AVAudioSession.sharedInstance().setCategory(.playback, options: .duckOthers)
  } catch(let error) {
    print(error.localizedDescription)
  }
  
  let utterance = AVSpeechUtterance(string: stringToSpeak)
  
  // Set voice to male or female based on user settings
  if defaults.integer(forKey: "Voice") == 1 {
    utterance.voice = AVSpeechSynthesisVoice(identifier: "com.apple.ttsbundle.siri_male_ja-JP_compact")
  }
  
  else {
    utterance.voice = AVSpeechSynthesisVoice(language: "ja-JP")
  }
  
  synthesizer.speak(utterance)