Why do sounds still play when mute/silent mode is ON?

I'm playing system sounds using AudioServicesPlaySystemSoundWithCompletion and custom sounds using AVAudioPlayer.


Why are sounds played if the silent switch is on? I've tried all the categories for the "AVAudioSession".


I tried the following code:


try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
try AVAudioSession.sharedInstance().setActive(true)

Also tried this:


try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers)
try AVAudioSession.sharedInstance().setActive(true)

I tried all the categories defined in this link, but the sounds are always playing when the phone is muted (silent switch ON). https://developer.apple.com/reference/avfoundation/avaudiosession/audio_session_categories


I'm playing the sounds like this:

System sounds:

AudioServicesPlaySystemSoundWithCompletion(1304, nil)


Custom sounds:


  let url = Bundle.main.url(forResource: "sound01", withExtension: ".wav") 
  do { 
       if audioPlayer != nil { 
            audioPlayer!.stop() 
            audioPlayer = nil 
       } 
       if let soundURL = soundURL { 
             try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: AVAudioSessionCategoryOptions.mixWithOthers)
             try AVAudioSession.sharedInstance().setActive(true) 
             audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
       } 
       guard let audioPlayer = audioPlayer else { return } 
       audioPlayer.prepareToPlay() 
       audioPlayer.play() 
  } 
  catch let error { print(error.localizedDescription) }




Am I doing something wrong?

hello


You can replace :


try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
try AVAudioSession.sharedInstance().setActive(true)


with a do try catch , and see what is wrong.


maybe you need to setActive(false) before setCategory()

maybe you need to stop audio before set new category...

Why do sounds still play when mute/silent mode is ON?
 
 
Q