How to check if sound off on iphone [duplicate] - iphone

I am developing an application. In that i want to detect through coding that "is iPhone on silent mode or not?". I am developing it by using cocoa with Objective-C.
If anyone knows it kindly reply.

The reason Pirripli's code does not work is that the simulator does not support the test and the code does not check for errors. Corrected code would look like:
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's possible by testing for a NULL audio route using AudioToolBox:
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
AudioSessionGetProperty (
kAudioSessionProperty_AudioRoute,
&routeSize,
&route
);
if (route == NULL) {
NSLog(#"Silent switch is on");
}
If route is NULL then there's no available audio outputs. If it's "Headset" or "Headphones" then the silent ringer switch could still be on. However, it will never be on when it's set to "Speaker".
You're probably best testing for this in your audio route change property listener, which is set below:
AudioSessionAddPropertyListener (
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,
self
);
Note: If you're doing anything funky like overriding audio routes, then this answer may not apply.
Setting up and tearing down an audio session in its entirety is probably beyond the scope of this answer.

For completeness, building off this link from Dan Bon, I implement the following method to solve this problem in my apps. One thing to note is that the code checks for the iPhone simulator first - executing the below code will crash the simulator. Anyone know why?
-(BOOL)silenced {
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
Declaring this right in the view controller, you'd simply check
if ([self silenced]) {
NSLog(#"silenced");
else {
NSLog(#"not silenced");
}
Or, obviously, you could declare it in some kind of helper class. A more elegant solution might be a category addition on UIApplication or some such other class...

You can use Audio Route property as suggested by the previous answers, but keep in mind that:
- It works only if the Audio Category is AmbientSound
- You should not initialize Audio Session more than once in your app (see Audio Session Programming Guide)
- You should release those CFStringRef to avoid mem leaks
In case the current audio category is not AmbientSound though, you can think of changing it temporarily, perform the check on Audio Route property, and then restoring the original Audio Category.
Note that changing Audio Category will restore the default Audio Route for that category, given the current hardware configuration (i.e. whether there are headphones plugged in or not, etc).

Related

iPhone: programmatically check if vibration is enabled

Is it possible to programmatically check if the system option of iPhone
Settings -> Sounds -> Vibrate on Ring
is enabled?
In my app, I would like to display an alert to the user if that option is disabled.
You cannot. Because apple is not providing the API to access the iPhone settings app.
may be you could give it a try and make sure you're running the app in iDevice because simulator don't have silent or ring mode :)
New Edits
-(BOOL)silenced
{
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
and you can call this method like this way
if ([self silenced])
{
NSLog(#"silenced");
} else {
NSLog(#"not silenced");
}
hope it will help you!

How can I mute incoming iPhone text messages programmatically?

I'm currently trying to use the AVSystemController private framework to mute system noises based on the user's selection. I'm currently muting phone calls by calling: [(AVSystemController object) setVolumeTo:0.0 forCategory:#"Ringtone"];
Is there a command to do that for incoming text messages? I imagine it would be based on a change in the category identified in that call. However, I can't find a list of categories to reference. Of the 10 I've been able to find (Alert, Audio/Video, Ringtone, Voicemail, VoicemailGreeting, PhoneCall, TTYCall, RingtonePreview, Alarm, Record), none of them govern text message sounds. Is there a category to do this? If not, is there any other way to mute the sound from incoming texts?
I realize this goes against Apple's no-private-frameworks policy, but this app won't go up on the app store so that's no problem. I'm developing it using the latest version of Xcode for the latest version of IOS, so any method to accomplish this would be doable.
#Jessica, You can't do that, bcos it's restricted. if you want to try it in your application, then your app might be Rejected in App store.
So, Using public APIs, it is not possible.
The link you found is using private APIs, which aren't documented or guaranteed to work the way you'd expect. If you tried to release an App Store app that called a private API, it would be automatically rejected.
if you want to Check , whether is silent or not, then use below code,
-(BOOL)silenced {
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
For completeness, building off this link from Dan Bon, I implement the following method to solve this problem in my apps. One thing to note is that the code checks for the iPhone simulator first - executing the below code will crash the simulator. Anyone know why?
-(BOOL)silenced {
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
if(CFStringGetLength(state) > 0)
return NO;
else
return YES;
}
Declaring this right in the view controller, you'd simply check
if ([self silenced]) {
NSLog(#"silenced");
else {
NSLog(#"not silenced");
}

Is there a way to determine the setting of the Ring/Silent switch on the iPhone

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 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.

How do you force audio to come out of the iPhone speaker when a headset is attached?

I'm trying to add a loudspeaker feature to one of my iPhone apps. I already created the recording functionality, but when I play the recorded audio it only plays to the phone headset.
What I need is the recorded file to be played on the loudspeaker, even if there is a headset attached. How could I reroute the audio to do this?
You need to override the default audio properties using AudioSessionSetProperty. Look at something like this to force all audio to go to the speaker (note that this will even happen if headphones are plugged in).
OSStatus err = 0;
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
err = AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute,sizeof(audioRouteOverride),&audioRouteOverride);
To detect the headphones, try this (this is literally copy/paste code off of another SO post, so caveat emptor, but it works for me):
/**
* Tells us if the headset is plugged in
*/
- (BOOL) headsetIsPluggedIn
{
BOOL returnVal = NO;
UInt32 routeSize = sizeof(CFStringRef);
CFStringRef route = NULL;
OSStatus error = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &routeSize, &route);
if (!error && (route != NULL) && ([(NSString*)route rangeOfString:#"Head"].location != NSNotFound))
{
CFRelease(route);
returnVal = YES;
}
return returnVal;
}
EDIT: There is a bit of a discussion in the comments about whether the CFRelease is appropriate or not. Any hardcore Core Foundation experts care to weigh in?

Detecting iPhone/iPod Touch Accessories

Is it possible to detect if the iPod Touch/iPhone has any headphones or other accessories connected to it?
I'm building an app that requires a microphone, and need to know if the "iSomething" has one connected or not, either via the dock connection, or using the headphone port, such as with the inline headphone/microphone accessory from Apple.
Finally found it - After initializing the Audio Session object, - AudioSessionInitialize() - you can make a call to AudioSessionGetProperty, and get the value of kAudioSessionProperty_AudioInputAvailable.
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 propertySize, micConnected;
AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &propertySize, &micConnected);
[self updateMicStatus:micConnected]; // user-created method
According to the docs for Audio Session Services, this should be used rather than using the device model (iPhone vs. iPod Touch) to determine if an audio input is available to use. You can also set up a callback function to monitor changes to this property via AudioSessionAddPropertyListener().
Not sure yet if this property also applies to devices connected via the Dock connector, but it appears to work for the headphone jack.
Or you could use:
if (![[AVAudioSession sharedInstance] inputIsAvailable]) {
// your code here for no audio input available
}
In IOS 6 inputIsAvailable is deprecated. In the future we need to use inputAvailable:
BOOL audioHWAvailable = audioSession.inputAvailable;
To determine if the device has a built in microphone you can just go by [UIDevice currentDevice].model to see if it's an iPhone or a 2nd generation iPod Touch. As far as a third-party microphone plugged into the dock connector, this is not possible in the current 2.2.1 SDK, but it may be in a later version :)
Here is the solution, you may like it or it is helpful to you.
Before using below method please write this two line also
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
- (void)isHeadsetPluggedIn {
UInt32 routeSize = sizeof (CFStringRef);
CFStringRef route;
AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
&routeSize,
&route);
//NSLog(#"Error >>>>>>>>>> :%#", error);
/* Known values of route:
* "Headset"
* "Headphone"
* "Speaker"
* "SpeakerAndMicrophone"
* "HeadphonesAndMicrophone"
* "HeadsetInOut"
* "ReceiverAndMicrophone"
* "Lineout"
*/
NSString* routeStr = (NSString*)route;
NSRange headsetRange = [routeStr rangeOfString : #"Headset"];
NSRange receiverRange = [routeStr rangeOfString : #"Receiver"];
if(headsetRange.location != NSNotFound) {
// Don't change the route if the headset is plugged in.
NSLog(#"headphone is plugged in ");
}
else if (receiverRange.location != NSNotFound) {
// Change to play on the speaker
NSLog(#"play on the speaker");
}
else {
NSLog(#"Unknown audio route.");
}
}