RSU

[Thursday, July 9, 2009]

iPhone Silent Mode/Switch state detection using SDK.

I wanted my app to "buzz" only when the phone was in silent mode, or in other words, the button was set to vibration. To find this out pragmatically you can use the following code snippet, that I figured out after some struggling with the API.



CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
NSLog(@"Vibration Off");
else
NSLog(@"Vibration On");

The string returned by querying the property kAudioSessionProperty_AudioRoute can be blank or Speaker. It can also be Lineout depending on if the phone is docked or not. So if its blank, length is 0, and hence no audio route is currently available. i.e. silent mode is active.

1 comments:

Anonymous | May 12, 2010 at 4:42 AM

It is useless if the category of your app's audio session is not kAudioSessionCategory_AmbientSound.

Post a Comment