views:

54

answers:

2

I have a mailing application. If the user sends a mail successfully, then I need to notify that the mail was sent successfully. For that, I need to know if his phone is on silent mode (in which case there will be a 'vibrate') or regular mode (in which case there will be a 'beep'). Can anyone help me with it?

Thanks in advance

A: 
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) == 0)
{
    //SILENT

}
else
{
    //NOT SILENT

}

If the state string is empty then the phone is on silent - otherwise the phone has an audio output

EDIT:

remember to add the AudioToolbox framework and import. – Thomas Clayson

answer taken from (http://iphone-dev-tips.alterplay.com/2009/12/iphone-silent-mode-detection.html)

Rick
ah... beat me to it... :p *deletes my answer*
Thomas Clayson
remember to add the *AudioToolbox* framework and import.
Thomas Clayson
yeah - sweet, i'll edit that in
Rick
where should I put this code?
Bogus Boy
You can either put it exactly where you want to use it (after you've sent the message). Or you could put in a method of its own called something like "BOOL phoneIsOnSilentMode()" and return the result of (CFStringGetLength(state) == 0). Then when you want to use it after sending the message call "if (phoneIsOnSilentMode())" and vibrate if true or play a sound if false
Rick
Hey I have put this snippet in -applicationdidFinishLaunching method and when I try running it on my iPhone, it runs only the first time after install or when I switch off the phone and try again. Why is this happening? Isn't it suppose to run everytime I launch my app. And one more question- the state variable, when I display it in alert, always shows 'speaker' even after my changing the sound settings. How many states are there and how to change them from sound settings of iPhone. That is to say that I could never get the state variable as null, it's always speaker.
Bogus Boy
The state of iPhone apps is sometimes stored - so the applicationDidFinishLaunching method isn't necessarily called every time you click on the app icon from the home screen. Regardless, you surely want to make the check just before either playing the sound or vibrating? Otherwise a user might just open your app, begin to use it and then change to silent before sending a message. Then once the message had been sent you would have the wrong information about the audio state. Put the snippet in its own method and call the method after sending the message and before playing the sound or vibrating
Rick
I got it Rick thanks a lot. But I was doing it just for the testing purpose. Anyways, let's assume that I put the code where it should be put but the issue I am facing is that the function is never called at all. I have tried putting my phone on silent mode and testing but it's not called. I am displaying an alert if it's in silent mode just for the sake of testing. So, either I don't know how to set the iPhone on silent mode or there is something else at play. Also, please tell me about the other modes. So that I can program for head phone on and all.
Bogus Boy
the mode is always "speaker" no matter whatever I do with the sound settings from my home screen
Bogus Boy
Are you using an iPhone or an iPod touch?
Rick
Rick
I am using iPhone
Bogus Boy
got it done rick--thanks a ton for bearing with me
Bogus Boy
A: 

If you use the correct audio session type, iOS will handle this for you:

http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html

Josh Hinman