Archive for July, 2009

Detect Mute Switch State on iPhone
July 8, 2009

You’re a developer! You’re the coolest person on your block! You want to know how to detect if the mute switch on the iPhone is flipped (ya know, for reminding dumb user why their phone isn’t playing any sound.) Well, I stumbled upon a solution – and I thought I’d share. Our goofy app, Wookiee Translator was about to be updated with this new detection before we pulled it from the store.

Here’s how it goes: If you’re calling a sound to play with AVAudioPlayer, and the mute switch is ON, the iPhone software doesn’t play anything, or waste time reading the audio file. (This might be true of OpenAL as well) If you create a “check” to see if anything is playing or not, you’ll get an indication of whether the switch is flipped or not!

In the Wookiee Translator, I set up a timer to calculate how long it was from when the sound file is called to play, to when it finishes playing. If that timer check returns a value less than the shortest sound files, then I know the mute switch is flipped, and can alert the user.

If your app isn’t as audio focussed, you could add a small silent audio file of a certain length, and call it to “play” and check go see if it does or not, and bingo – there’s your hardware switch state – do with it what you will.

And please, leave a comment if this is helpful!