I am trying to figure out from my code whether the Ring/Silent switch is on ring or silent. Is there a way to determine this from 开发者_JAVA技巧my program.
Thanks
I did some more searching and found the same question here How to detect iphone is on silent mode.
For completeness, here is the answer from Neil that worked for me?
CFStringRef state = nil;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
OSStatus status = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if (status == kAudioSessionNoError) {
return (CFStringGetLength(state) == 0); // YES = silent
}
return NO;
It should be noted that this will not work if headphones are connected. You will always get "HeadPhone". This was also reported by coob.
The answer is no.
You can configure how your audio session is affected by the switch, but you cannot tell whether it's on or off.
精彩评论