18

I've been looking for hours and I can't seem to find any API that explains how to detect whether an iPhone ringer is in silent mode or not. This is what I'm talking about:

iPhone Ringer Switch

Is there anyway I can find out whether an iOS device's silent mode is on or off using Swift code? I'm specifically looking for iOS8 and up since the iOS5 ones have been deprecated. Thanks.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
BunkBedNoob
  • 367
  • 1
  • 4
  • 15
  • Have u checked this http://stackoverflow.com/a/20992650/5362916 – Uma Madhavi Feb 09 '16 at 05:46
  • I have and the top answer said that Apple didn't build it yet. Yet I've seen Snapchat and other apps mute/unmute their video playback when the mute button is on/off. How do they do it? Also that answer was for iOS 4... @Uma – BunkBedNoob Feb 09 '16 at 05:51

1 Answers1

17

I don't think there is and you probably don't need to directly call an API to detect if the device is muted or not. What you need to know is this:

When playing a sound, you will do something like:

        try AVAudioSession.sharedInstance().setCategory({AVAudioSessionCategory})
        try AVAudioSession.sharedInstance().setActive(true)
        audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
        audioPlayer.prepareToPlay()
        audioPlayer.play()

In the {AVAudioSessionCategory}, you can specify:

AVAudioSessionCategorySoloAmbient: Your audio is silenced by screen locking and by the Silent switch

AVAudioSessionCategoryPlayBack: Your Audio continues with the Silent switch set to silent or when the screen locks

Guy Daher
  • 5,526
  • 5
  • 42
  • 67