Reading the Silent Switch?

Member
Posts: 268
Joined: 2005.04
Post: #1
Is it possible to read the state of the silent switch on the side of the phone? I can't seem to find anything in the docs or SDK.
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #2
its a bit tricky, you have to set your output to ambient level sound then it will respect the silent switch. There is no true way to "read" it though.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #3
Also note that setting the session to ambient works fine with OpenAL, but not with Audio Queues. With Audio Queues, the system apparently tries to pipe audio through the same single hardware decoder as iTunes, if the user had played music, or turns on iTunes during your game (double-clicking the home button). This can freeze the device, requiring a reboot!
Quote this message in a reply
Member
Posts: 268
Joined: 2005.04
Post: #4
Sweet, that's exactly what I needed. And it solved another issue with the game audio stopping any iPod playback. Here's the code I used for anyone searching in the future. Just stick it before any OAL initialization.

https://developer.apple.com/iphone/libra...rence.html
https://developer.apple.com/iphone/libra...SpeakHere/

Code:
// The audio session setup
UInt32    sessionCategory = kAudioSessionCategory_AmbientSound;
    
AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(YES);

// The interruption callback
void interruptionListenerCallback(void *inUserData, UInt32 interruptionState)
{
    MySoundController    *controller = (MySoundController *)inUserData;
    
    if (interruptionState == kAudioSessionBeginInterruption)
    {
        // stop sound/music here
        [controller stopPlaying]; // or whatever
    }
    else if (interruptionState == kAudioSessionEndInterruption)
    {
        // resume any sound/music necessary here
        [controller resumePlaying]; // etc
    }
}
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #5
There is a rumor that 2.2 will fix the silent switch issue and give us an easy way to check for it.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Member
Posts: 268
Joined: 2005.04
Post: #6
This page is also required reading for any app that wants to play audio:

Audio Sessions: Cooperating with Core Audio

That link should be in big huge +10 font on the iPhone dev homepage.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How do I get iPod Player to respect silent switch? CVogelbusch 0 1,623 Nov 25, 2009 05:34 AM
Last Post: CVogelbusch
  Added openfient, switch to c++ duplicate symbol kendric 7 3,455 Aug 9, 2009 02:47 AM
Last Post: backslash