1

I have to create an app in which the iPhone goes silent upon a button press event.

How can you do this programatically?

4 Answers 4

6

There is nothing in official iOS SDK to do this. Imagine someone miss an important call because an app changed settings and made phone silent without user's knowledge. I don't want to download that application for sure. See this related question too.

From Apple's documentation

People, not applications, should initiate and control actions. Although an application can suggest a course of action or warn about dangerous consequences, it’s usually a mistake for the app to take decision-making away from the user. The best apps find the correct balance between giving people the capabilities they need while helping them avoid dangerous outcomes.

If I am not mistaken, making phone silent is one these kind of action.

Read the sound section of apple documentation.

EDIT : If you want more information.

Go to apple developer forum (You must have a login), and see this thread. The guy who answers there is an apple employee.

6
  • as per client requirment ,after press on silent button only incoming call volume get silent .
    – triveni
    Sep 5, 2011 at 10:48
  • What you want, your app to go silent (stop all sounds produced by your app) or entire iphone go silent? Sep 5, 2011 at 10:54
  • on click event of buttion entire iphone go silent.
    – triveni
    Sep 5, 2011 at 12:10
  • Using official iphone sdk you cannot achieve that..I dont know whether it is possible using any other private api's..But I wouldn't go for that if I were you..You want apple to approve your app right? Sep 5, 2011 at 12:13
  • So if I get this right. Apple restricts users in a specific way of thinking thus not allowing people to thinks differently... Wow, this company sucks. Lack of innovation... May 6, 2018 at 23:34
2
// "Ambient" makes it respect the mute switch
// Must call this once to init session
if (!gAudioSessionInited)
{
    AudioSessionInterruptionListener    inInterruptionListener = NULL;
    OSStatus    error;
    if ((error = AudioSessionInitialize (NULL, NULL, inInterruptionListener, NULL)))
    {
        NSLog(@"*** Error *** error in AudioSessionInitialize: %d.", error);
    }
    else
    {
        gAudioSessionInited = YES;
    }
}

SInt32  ambient = kAudioSessionCategory_AmbientSound;
if (AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (ambient), &ambient))
{
        NSLog(@"*** Error *** could not set Session property to ambient.");
}

Hope this will help you ...

2
  • no ,try this also but not help.this is not my proper solution
    – triveni
    Sep 5, 2011 at 10:52
  • ya u right this is not working if we aren't in the ambient mode,where the mute switch is respected. check the next code i post 4 u...
    – user755278
    Sep 5, 2011 at 10:59
1
-(BOOL)muteSwitchEnabled {

#if TARGET_IPHONE_SIMULATOR
    // set to NO in simulator. Code causes crashes for some reason.
    return NO;
#endif

// go back to Ambient to detect the switch
AVAudioSession* sharedSession = [AVAudioSession sharedInstance];
[sharedSession setCategory:AVAudioSessionCategoryAmbient error:nil];

CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);

BOOL muteSwitch = (CFStringGetLength(state) <= 0);
NSLog(@"Mute switch: %d",muteSwitch);

// code below here is just restoring my own audio state, YMMV
_hasMicrophone = [sharedSession inputIsAvailable];
NSError* setCategoryError = nil;

if (_hasMicrophone) {

    [sharedSession setCategory: AVAudioSessionCategoryPlayAndRecord error: &setCategoryError];

    // By default PlayAndRecord plays out over the internal speaker.  We want the external speakers, thanks.
    UInt32 ASRoute = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
                             sizeof (ASRoute),
                             &ASRoute
                             );
}
else
    // Devices with no mike don't support PlayAndRecord - we don't get playback, so use just playback as we don't have a microphone anyway
    [sharedSession setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];

if (setCategoryError)
    NSLog(@"Error setting audio category! %@", setCategoryError);

return muteSwitch;
}

first switch to ambient, read the switch, and then return to the settings ...

3
  • Dont post multiple answers in thread...Edit your original post if you want to change Sep 5, 2011 at 11:10
  • I just wrote separately to look clear. I'll be careful next time .Thanks
    – user755278
    Sep 5, 2011 at 11:19
  • how can you say like that Krishna If you were not true then why did you give Upwote i just help you in your problem. i accept the downvote rather than deleting the post. may be this will help someone in future.
    – user755278
    Sep 5, 2011 at 11:36
0

there is no public api is open for the developers because when your app is running and you got a call then your app will quit or may be in background but you cann't make any changes to device .because call is also made on the system level event

2
  • i have to create apps in which iphone get silent on click event iphone goes silent profile mode...
    – triveni
    Sep 5, 2011 at 11:02
  • but by doing this the result will be an app that is having private framework and apple will reject the app .If still you want it then read the how to unlock private framework (ERICA SADUN) in iphone that will help you to use the private framework that might provide you this functionality
    – B25Dec
    Sep 6, 2011 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.