How to programmatically sense the iPhone mute switch?












44















I can't seem to find in the SDK how to programatically sense the mute button/switch on the iPhone. When my app plays background music, it responds properly to the volume button without me having any code to follow that but, when I use the mute switch, it just keeps playing away.



How do I test the position of mute?



(NOTE: My program has its own mute switch, but I'd like the physical switch to override that.)



Thanks!










share|improve this question





























    44















    I can't seem to find in the SDK how to programatically sense the mute button/switch on the iPhone. When my app plays background music, it responds properly to the volume button without me having any code to follow that but, when I use the mute switch, it just keeps playing away.



    How do I test the position of mute?



    (NOTE: My program has its own mute switch, but I'd like the physical switch to override that.)



    Thanks!










    share|improve this question



























      44












      44








      44


      47






      I can't seem to find in the SDK how to programatically sense the mute button/switch on the iPhone. When my app plays background music, it responds properly to the volume button without me having any code to follow that but, when I use the mute switch, it just keeps playing away.



      How do I test the position of mute?



      (NOTE: My program has its own mute switch, but I'd like the physical switch to override that.)



      Thanks!










      share|improve this question
















      I can't seem to find in the SDK how to programatically sense the mute button/switch on the iPhone. When my app plays background music, it responds properly to the volume button without me having any code to follow that but, when I use the mute switch, it just keeps playing away.



      How do I test the position of mute?



      (NOTE: My program has its own mute switch, but I'd like the physical switch to override that.)



      Thanks!







      iphone






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 2 '11 at 10:21









      Jane Sales

      13k34756




      13k34756










      asked Nov 13 '08 at 17:09









      OlieOlie

      18.3k1785126




      18.3k1785126
























          10 Answers
          10






          active

          oldest

          votes


















          29














          Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...



          // "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.");
          }





          share|improve this answer
























          • if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?

            – Finn Gaida
            Nov 14 '13 at 22:01











          • Does not seem to work on iOS 8.

            – Andres Canella
            Jun 12 '15 at 19:55











          • "Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!

            – Olie
            Jun 13 '15 at 1:30











          • Could you please help me: stackoverflow.com/questions/31604629/…

            – Ganee....
            Aug 6 '15 at 8:09



















          10














          I answered a similar question here (link). The relevant 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;

          }





          share|improve this answer





















          • 1





            This works great.

            – Gerald Kaszuba
            Mar 14 '11 at 22:47











          • Exactly what I was looking for. Thanks Pirripli!

            – James Skidmore
            Mar 23 '11 at 4:55






          • 1





            This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.

            – yood
            May 6 '11 at 22:17











          • But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'

            – Chris Ladd
            May 20 '11 at 22:22






          • 5





            No longer works in iOS 5. stackoverflow.com/questions/6901363/…

            – morgancodes
            Nov 5 '11 at 17:07



















          7














          Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.



          I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.



          -(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;
          }





          share|improve this answer

































            5














            To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.



            -(NSString*)audioRoute
            {
            CFStringRef state;
            UInt32 propertySize = sizeof(CFStringRef);
            OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
            if( n )
            {
            // TODO: Throw an exception
            NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
            }

            NSString *result = (NSString*)state;
            [result autorelease];
            return result;
            }

            -(Float32)audioVolume
            {
            Float32 state;
            UInt32 propertySize = sizeof(CFStringRef);
            OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
            if( n )
            {
            // TODO: Throw an exception
            NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
            }
            return state;
            }





            share|improve this answer































              5














              -(BOOL)isDeviceMuted
              {
              CFStringRef state;
              UInt32 propertySize = sizeof(CFStringRef);
              AudioSessionInitialize(NULL, NULL, NULL, NULL);
              AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
              return (CFStringGetLength(state) > 0 ? NO : YES);
              }





              share|improve this answer





















              • 3





                (CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)

                – Daniel
                Oct 19 '10 at 12:52











              • @simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).

                – Yuvaraj.M
                Nov 24 '12 at 13:16











              • @Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.

                – Daniel
                Nov 26 '12 at 10:21











              • @simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.

                – Yuvaraj.M
                Nov 26 '12 at 12:01



















              4














              Olie,



              I believe you can find the answer to your question here:



              https://devforums.apple.com/message/1135#1135



              I'm assuming you have access to the Developer Forums at Apple.com :)






              share|improve this answer


























              • Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!

                – Olie
                Nov 15 '08 at 7:42











              • This comment is not very helpful. Can you at least post a quote from the link you posted?

                – der_michael
                Nov 20 '18 at 18:16



















              4














              I followed the general theory here and got this to work
              http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/



              Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.






              share|improve this answer































                3














                Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.



                The code in application:didFinishLaunchingWithOptions:



                NSError *error = nil;
                [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
                [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
                [[AVAudioSession sharedInstance] setActive:YES error:&error];


                The code in viewWillAppear on cameraController, if you have to use camera or recording in your app



                [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];


                The code in viewWillDisappear on cameraController



                [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];


                Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!






                share|improve this answer
























                • Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.

                  – Ganee....
                  Jul 30 '15 at 7:31



















                2















                For Swift




                Below framework works perfectly in device



                https://github.com/akramhussein/Mute



                Just install using pod or download from Git



                pod 'Mute'


                and use like below code



                import UIKit
                import Mute

                class ViewController: UIViewController {

                @IBOutlet weak var label: UILabel! {
                didSet {
                self.label.text = ""
                }
                }

                override func viewDidLoad() {
                super.viewDidLoad()

                // Notify every 2 seconds
                Mute.shared.checkInterval = 2.0

                // Always notify on interval
                Mute.shared.alwaysNotify = true

                // Update label when notification received
                Mute.shared.notify = { m in
                self.label.text = m ? "Muted" : "Not Muted"
                }

                // Stop after 5 seconds
                DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                Mute.shared.isPaused = true
                }

                // Re-start after 10 seconds
                DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
                Mute.shared.isPaused = false
                }
                }

                }





                share|improve this answer































                  1














                  Here two examples how to use AudioSessionInitialize:
                  http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/






                  share|improve this answer
























                  • Link is broken.

                    – Daniel Springer
                    Nov 6 '18 at 22:24











                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f287543%2fhow-to-programmatically-sense-the-iphone-mute-switch%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  10 Answers
                  10






                  active

                  oldest

                  votes








                  10 Answers
                  10






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  29














                  Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...



                  // "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.");
                  }





                  share|improve this answer
























                  • if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?

                    – Finn Gaida
                    Nov 14 '13 at 22:01











                  • Does not seem to work on iOS 8.

                    – Andres Canella
                    Jun 12 '15 at 19:55











                  • "Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!

                    – Olie
                    Jun 13 '15 at 1:30











                  • Could you please help me: stackoverflow.com/questions/31604629/…

                    – Ganee....
                    Aug 6 '15 at 8:09
















                  29














                  Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...



                  // "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.");
                  }





                  share|improve this answer
























                  • if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?

                    – Finn Gaida
                    Nov 14 '13 at 22:01











                  • Does not seem to work on iOS 8.

                    – Andres Canella
                    Jun 12 '15 at 19:55











                  • "Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!

                    – Olie
                    Jun 13 '15 at 1:30











                  • Could you please help me: stackoverflow.com/questions/31604629/…

                    – Ganee....
                    Aug 6 '15 at 8:09














                  29












                  29








                  29







                  Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...



                  // "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.");
                  }





                  share|improve this answer













                  Thanks, JPM. Indeed, the link you provide leads to the correct answer (eventually. ;) For completeness (because S.O. should be a source of QUICK answers! )...



                  // "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.");
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '08 at 19:03









                  OlieOlie

                  18.3k1785126




                  18.3k1785126













                  • if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?

                    – Finn Gaida
                    Nov 14 '13 at 22:01











                  • Does not seem to work on iOS 8.

                    – Andres Canella
                    Jun 12 '15 at 19:55











                  • "Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!

                    – Olie
                    Jun 13 '15 at 1:30











                  • Could you please help me: stackoverflow.com/questions/31604629/…

                    – Ganee....
                    Aug 6 '15 at 8:09



















                  • if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?

                    – Finn Gaida
                    Nov 14 '13 at 22:01











                  • Does not seem to work on iOS 8.

                    – Andres Canella
                    Jun 12 '15 at 19:55











                  • "Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!

                    – Olie
                    Jun 13 '15 at 1:30











                  • Could you please help me: stackoverflow.com/questions/31604629/…

                    – Ganee....
                    Aug 6 '15 at 8:09

















                  if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?

                  – Finn Gaida
                  Nov 14 '13 at 22:01





                  if I log the value of ambient afterwards, I will always get returned 1634558569 no matter if I'm muted or not. Any idea?

                  – Finn Gaida
                  Nov 14 '13 at 22:01













                  Does not seem to work on iOS 8.

                  – Andres Canella
                  Jun 12 '15 at 19:55





                  Does not seem to work on iOS 8.

                  – Andres Canella
                  Jun 12 '15 at 19:55













                  "Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!

                  – Olie
                  Jun 13 '15 at 1:30





                  "Does not work" how? What did you do? What did you expect to happen? What happened, instead? If you provide more info, I'll try to update my answer. Thanks!

                  – Olie
                  Jun 13 '15 at 1:30













                  Could you please help me: stackoverflow.com/questions/31604629/…

                  – Ganee....
                  Aug 6 '15 at 8:09





                  Could you please help me: stackoverflow.com/questions/31604629/…

                  – Ganee....
                  Aug 6 '15 at 8:09













                  10














                  I answered a similar question here (link). The relevant 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;

                  }





                  share|improve this answer





















                  • 1





                    This works great.

                    – Gerald Kaszuba
                    Mar 14 '11 at 22:47











                  • Exactly what I was looking for. Thanks Pirripli!

                    – James Skidmore
                    Mar 23 '11 at 4:55






                  • 1





                    This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.

                    – yood
                    May 6 '11 at 22:17











                  • But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'

                    – Chris Ladd
                    May 20 '11 at 22:22






                  • 5





                    No longer works in iOS 5. stackoverflow.com/questions/6901363/…

                    – morgancodes
                    Nov 5 '11 at 17:07
















                  10














                  I answered a similar question here (link). The relevant 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;

                  }





                  share|improve this answer





















                  • 1





                    This works great.

                    – Gerald Kaszuba
                    Mar 14 '11 at 22:47











                  • Exactly what I was looking for. Thanks Pirripli!

                    – James Skidmore
                    Mar 23 '11 at 4:55






                  • 1





                    This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.

                    – yood
                    May 6 '11 at 22:17











                  • But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'

                    – Chris Ladd
                    May 20 '11 at 22:22






                  • 5





                    No longer works in iOS 5. stackoverflow.com/questions/6901363/…

                    – morgancodes
                    Nov 5 '11 at 17:07














                  10












                  10








                  10







                  I answered a similar question here (link). The relevant 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;

                  }





                  share|improve this answer















                  I answered a similar question here (link). The relevant 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;

                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 23 '17 at 11:46









                  Community

                  11




                  11










                  answered Jan 16 '11 at 16:47









                  Chris LaddChris Ladd

                  2,06512222




                  2,06512222








                  • 1





                    This works great.

                    – Gerald Kaszuba
                    Mar 14 '11 at 22:47











                  • Exactly what I was looking for. Thanks Pirripli!

                    – James Skidmore
                    Mar 23 '11 at 4:55






                  • 1





                    This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.

                    – yood
                    May 6 '11 at 22:17











                  • But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'

                    – Chris Ladd
                    May 20 '11 at 22:22






                  • 5





                    No longer works in iOS 5. stackoverflow.com/questions/6901363/…

                    – morgancodes
                    Nov 5 '11 at 17:07














                  • 1





                    This works great.

                    – Gerald Kaszuba
                    Mar 14 '11 at 22:47











                  • Exactly what I was looking for. Thanks Pirripli!

                    – James Skidmore
                    Mar 23 '11 at 4:55






                  • 1





                    This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.

                    – yood
                    May 6 '11 at 22:17











                  • But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'

                    – Chris Ladd
                    May 20 '11 at 22:22






                  • 5





                    No longer works in iOS 5. stackoverflow.com/questions/6901363/…

                    – morgancodes
                    Nov 5 '11 at 17:07








                  1




                  1





                  This works great.

                  – Gerald Kaszuba
                  Mar 14 '11 at 22:47





                  This works great.

                  – Gerald Kaszuba
                  Mar 14 '11 at 22:47













                  Exactly what I was looking for. Thanks Pirripli!

                  – James Skidmore
                  Mar 23 '11 at 4:55





                  Exactly what I was looking for. Thanks Pirripli!

                  – James Skidmore
                  Mar 23 '11 at 4:55




                  1




                  1





                  This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.

                  – yood
                  May 6 '11 at 22:17





                  This doesn't work when headphones are plugged in. AudioSessionGetProperty for the audio route returns 'Headphone' regardless of mute switch.

                  – yood
                  May 6 '11 at 22:17













                  But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'

                  – Chris Ladd
                  May 20 '11 at 22:22





                  But does it matter if the mute switch is on when headphones are plugged in? Audio still plays out of the headphones, yes? It seems to me the point of the mute switch is to silence the phone from others, not yourself. Obeying it shouldn't matter much (imo) while headphones are in. That seems to be the stance Apple has taken by returning 'Headphone'

                  – Chris Ladd
                  May 20 '11 at 22:22




                  5




                  5





                  No longer works in iOS 5. stackoverflow.com/questions/6901363/…

                  – morgancodes
                  Nov 5 '11 at 17:07





                  No longer works in iOS 5. stackoverflow.com/questions/6901363/…

                  – morgancodes
                  Nov 5 '11 at 17:07











                  7














                  Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.



                  I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.



                  -(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;
                  }





                  share|improve this answer






























                    7














                    Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.



                    I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.



                    -(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;
                    }





                    share|improve this answer




























                      7












                      7








                      7







                      Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.



                      I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.



                      -(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;
                      }





                      share|improve this answer















                      Some of the code in other answers (including the accepted answer) may not work if you aren't in the ambient mode, where the mute switch is respected.



                      I wrote the routine below to switch to ambient, read the switch, and then return to the settings I need in my app.



                      -(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;
                      }






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 21 '12 at 9:15









                      Praveen-K

                      3,21111730




                      3,21111730










                      answered Aug 2 '11 at 10:24









                      Jane SalesJane Sales

                      13k34756




                      13k34756























                          5














                          To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.



                          -(NSString*)audioRoute
                          {
                          CFStringRef state;
                          UInt32 propertySize = sizeof(CFStringRef);
                          OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
                          if( n )
                          {
                          // TODO: Throw an exception
                          NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
                          }

                          NSString *result = (NSString*)state;
                          [result autorelease];
                          return result;
                          }

                          -(Float32)audioVolume
                          {
                          Float32 state;
                          UInt32 propertySize = sizeof(CFStringRef);
                          OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
                          if( n )
                          {
                          // TODO: Throw an exception
                          NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
                          }
                          return state;
                          }





                          share|improve this answer




























                            5














                            To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.



                            -(NSString*)audioRoute
                            {
                            CFStringRef state;
                            UInt32 propertySize = sizeof(CFStringRef);
                            OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
                            if( n )
                            {
                            // TODO: Throw an exception
                            NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
                            }

                            NSString *result = (NSString*)state;
                            [result autorelease];
                            return result;
                            }

                            -(Float32)audioVolume
                            {
                            Float32 state;
                            UInt32 propertySize = sizeof(CFStringRef);
                            OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
                            if( n )
                            {
                            // TODO: Throw an exception
                            NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
                            }
                            return state;
                            }





                            share|improve this answer


























                              5












                              5








                              5







                              To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.



                              -(NSString*)audioRoute
                              {
                              CFStringRef state;
                              UInt32 propertySize = sizeof(CFStringRef);
                              OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
                              if( n )
                              {
                              // TODO: Throw an exception
                              NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
                              }

                              NSString *result = (NSString*)state;
                              [result autorelease];
                              return result;
                              }

                              -(Float32)audioVolume
                              {
                              Float32 state;
                              UInt32 propertySize = sizeof(CFStringRef);
                              OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
                              if( n )
                              {
                              // TODO: Throw an exception
                              NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
                              }
                              return state;
                              }





                              share|improve this answer













                              To find out the state of the mute switch and the volume control I wrote these two functions. These are ideal if you wish to warn the user before they try creating audio output.



                              -(NSString*)audioRoute
                              {
                              CFStringRef state;
                              UInt32 propertySize = sizeof(CFStringRef);
                              OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
                              if( n )
                              {
                              // TODO: Throw an exception
                              NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
                              }

                              NSString *result = (NSString*)state;
                              [result autorelease];
                              return result;
                              }

                              -(Float32)audioVolume
                              {
                              Float32 state;
                              UInt32 propertySize = sizeof(CFStringRef);
                              OSStatus n = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, &propertySize, &state);
                              if( n )
                              {
                              // TODO: Throw an exception
                              NSLog( @"AudioSessionGetProperty: %@", osString( n ) );
                              }
                              return state;
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 3 '10 at 15:14









                              Martin CowieMartin Cowie

                              2,20652868




                              2,20652868























                                  5














                                  -(BOOL)isDeviceMuted
                                  {
                                  CFStringRef state;
                                  UInt32 propertySize = sizeof(CFStringRef);
                                  AudioSessionInitialize(NULL, NULL, NULL, NULL);
                                  AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
                                  return (CFStringGetLength(state) > 0 ? NO : YES);
                                  }





                                  share|improve this answer





















                                  • 3





                                    (CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)

                                    – Daniel
                                    Oct 19 '10 at 12:52











                                  • @simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).

                                    – Yuvaraj.M
                                    Nov 24 '12 at 13:16











                                  • @Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.

                                    – Daniel
                                    Nov 26 '12 at 10:21











                                  • @simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.

                                    – Yuvaraj.M
                                    Nov 26 '12 at 12:01
















                                  5














                                  -(BOOL)isDeviceMuted
                                  {
                                  CFStringRef state;
                                  UInt32 propertySize = sizeof(CFStringRef);
                                  AudioSessionInitialize(NULL, NULL, NULL, NULL);
                                  AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
                                  return (CFStringGetLength(state) > 0 ? NO : YES);
                                  }





                                  share|improve this answer





















                                  • 3





                                    (CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)

                                    – Daniel
                                    Oct 19 '10 at 12:52











                                  • @simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).

                                    – Yuvaraj.M
                                    Nov 24 '12 at 13:16











                                  • @Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.

                                    – Daniel
                                    Nov 26 '12 at 10:21











                                  • @simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.

                                    – Yuvaraj.M
                                    Nov 26 '12 at 12:01














                                  5












                                  5








                                  5







                                  -(BOOL)isDeviceMuted
                                  {
                                  CFStringRef state;
                                  UInt32 propertySize = sizeof(CFStringRef);
                                  AudioSessionInitialize(NULL, NULL, NULL, NULL);
                                  AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
                                  return (CFStringGetLength(state) > 0 ? NO : YES);
                                  }





                                  share|improve this answer















                                  -(BOOL)isDeviceMuted
                                  {
                                  CFStringRef state;
                                  UInt32 propertySize = sizeof(CFStringRef);
                                  AudioSessionInitialize(NULL, NULL, NULL, NULL);
                                  AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
                                  return (CFStringGetLength(state) > 0 ? NO : YES);
                                  }






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Mar 14 '11 at 22:44









                                  Gerald Kaszuba

                                  20.6k1699138




                                  20.6k1699138










                                  answered Aug 5 '10 at 18:56









                                  Haemish GrahamHaemish Graham

                                  5111




                                  5111








                                  • 3





                                    (CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)

                                    – Daniel
                                    Oct 19 '10 at 12:52











                                  • @simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).

                                    – Yuvaraj.M
                                    Nov 24 '12 at 13:16











                                  • @Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.

                                    – Daniel
                                    Nov 26 '12 at 10:21











                                  • @simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.

                                    – Yuvaraj.M
                                    Nov 26 '12 at 12:01














                                  • 3





                                    (CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)

                                    – Daniel
                                    Oct 19 '10 at 12:52











                                  • @simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).

                                    – Yuvaraj.M
                                    Nov 24 '12 at 13:16











                                  • @Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.

                                    – Daniel
                                    Nov 26 '12 at 10:21











                                  • @simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.

                                    – Yuvaraj.M
                                    Nov 26 '12 at 12:01








                                  3




                                  3





                                  (CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)

                                  – Daniel
                                  Oct 19 '10 at 12:52





                                  (CFStringGetLength(state) > 0 ? NO : YES) is the same as (CFStringGetLength(state) <= 0)

                                  – Daniel
                                  Oct 19 '10 at 12:52













                                  @simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).

                                  – Yuvaraj.M
                                  Nov 24 '12 at 13:16





                                  @simpleBob how to detect the iphone mute button value. (e.x if the iphone mute switch in on position (in mute) how to detect the mute value).

                                  – Yuvaraj.M
                                  Nov 24 '12 at 13:16













                                  @Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.

                                  – Daniel
                                  Nov 26 '12 at 10:21





                                  @Yuvaraj.M That is what this question is about. Select your favorite answer ;) For example you could just run the method above, which returns YES if the device is muted.

                                  – Daniel
                                  Nov 26 '12 at 10:21













                                  @simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.

                                  – Yuvaraj.M
                                  Nov 26 '12 at 12:01





                                  @simpleBob Thanks for reply. ANd Sorry for the disturb. It is working for all ios? It is not working in my ios5?.

                                  – Yuvaraj.M
                                  Nov 26 '12 at 12:01











                                  4














                                  Olie,



                                  I believe you can find the answer to your question here:



                                  https://devforums.apple.com/message/1135#1135



                                  I'm assuming you have access to the Developer Forums at Apple.com :)






                                  share|improve this answer


























                                  • Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!

                                    – Olie
                                    Nov 15 '08 at 7:42











                                  • This comment is not very helpful. Can you at least post a quote from the link you posted?

                                    – der_michael
                                    Nov 20 '18 at 18:16
















                                  4














                                  Olie,



                                  I believe you can find the answer to your question here:



                                  https://devforums.apple.com/message/1135#1135



                                  I'm assuming you have access to the Developer Forums at Apple.com :)






                                  share|improve this answer


























                                  • Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!

                                    – Olie
                                    Nov 15 '08 at 7:42











                                  • This comment is not very helpful. Can you at least post a quote from the link you posted?

                                    – der_michael
                                    Nov 20 '18 at 18:16














                                  4












                                  4








                                  4







                                  Olie,



                                  I believe you can find the answer to your question here:



                                  https://devforums.apple.com/message/1135#1135



                                  I'm assuming you have access to the Developer Forums at Apple.com :)






                                  share|improve this answer















                                  Olie,



                                  I believe you can find the answer to your question here:



                                  https://devforums.apple.com/message/1135#1135



                                  I'm assuming you have access to the Developer Forums at Apple.com :)







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 13 '08 at 20:33

























                                  answered Nov 13 '08 at 20:15









                                  jpmjpm

                                  7,938335866




                                  7,938335866













                                  • Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!

                                    – Olie
                                    Nov 15 '08 at 7:42











                                  • This comment is not very helpful. Can you at least post a quote from the link you posted?

                                    – der_michael
                                    Nov 20 '18 at 18:16



















                                  • Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!

                                    – Olie
                                    Nov 15 '08 at 7:42











                                  • This comment is not very helpful. Can you at least post a quote from the link you posted?

                                    – der_michael
                                    Nov 20 '18 at 18:16

















                                  Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!

                                  – Olie
                                  Nov 15 '08 at 7:42





                                  Sho' 'nuff! Quick recap: "AudioSessionSetProperty to set the AudioCategory to Ambient" (I believe it's ok to talk about shipping software...) Thanks!

                                  – Olie
                                  Nov 15 '08 at 7:42













                                  This comment is not very helpful. Can you at least post a quote from the link you posted?

                                  – der_michael
                                  Nov 20 '18 at 18:16





                                  This comment is not very helpful. Can you at least post a quote from the link you posted?

                                  – der_michael
                                  Nov 20 '18 at 18:16











                                  4














                                  I followed the general theory here and got this to work
                                  http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/



                                  Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.






                                  share|improve this answer




























                                    4














                                    I followed the general theory here and got this to work
                                    http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/



                                    Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.






                                    share|improve this answer


























                                      4












                                      4








                                      4







                                      I followed the general theory here and got this to work
                                      http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/



                                      Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.






                                      share|improve this answer













                                      I followed the general theory here and got this to work
                                      http://inforceapps.wordpress.com/2009/07/08/detect-mute-switch-state-on-iphone/



                                      Here is a recap: Play a short silent sound. Time how long it takes to play. If the mute switch is on, the playing of the sound will come back much shorter than the sound itself. I used a 500ms sound and if the sound played in less than this time, then the mute switch was on. I use Audio Services to play the silent sound (which always honors the mute switch). This article says that you can use AVAudioPlayer to play this sound. If you use AVAudioPlayer, I assume you'll need to setup your AVAudioSession's category to honor the mute switch, but I have not tried it`.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Apr 22 '10 at 5:43









                                      Mark24x7Mark24x7

                                      1,3321109




                                      1,3321109























                                          3














                                          Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.



                                          The code in application:didFinishLaunchingWithOptions:



                                          NSError *error = nil;
                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
                                          [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
                                          [[AVAudioSession sharedInstance] setActive:YES error:&error];


                                          The code in viewWillAppear on cameraController, if you have to use camera or recording in your app



                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];


                                          The code in viewWillDisappear on cameraController



                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];


                                          Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!






                                          share|improve this answer
























                                          • Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.

                                            – Ganee....
                                            Jul 30 '15 at 7:31
















                                          3














                                          Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.



                                          The code in application:didFinishLaunchingWithOptions:



                                          NSError *error = nil;
                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
                                          [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
                                          [[AVAudioSession sharedInstance] setActive:YES error:&error];


                                          The code in viewWillAppear on cameraController, if you have to use camera or recording in your app



                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];


                                          The code in viewWillDisappear on cameraController



                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];


                                          Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!






                                          share|improve this answer
























                                          • Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.

                                            – Ganee....
                                            Jul 30 '15 at 7:31














                                          3












                                          3








                                          3







                                          Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.



                                          The code in application:didFinishLaunchingWithOptions:



                                          NSError *error = nil;
                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
                                          [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
                                          [[AVAudioSession sharedInstance] setActive:YES error:&error];


                                          The code in viewWillAppear on cameraController, if you have to use camera or recording in your app



                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];


                                          The code in viewWillDisappear on cameraController



                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];


                                          Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!






                                          share|improve this answer













                                          Using Ambient mode for playing a video and PlayAndRecord mode for recording a video on camera screen, resolves the issue in our case.



                                          The code in application:didFinishLaunchingWithOptions:



                                          NSError *error = nil;
                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
                                          [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeVideoRecording error:&error];
                                          [[AVAudioSession sharedInstance] setActive:YES error:&error];


                                          The code in viewWillAppear on cameraController, if you have to use camera or recording in your app



                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];


                                          The code in viewWillDisappear on cameraController



                                          [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];


                                          Using these lines our Application records and plays a video and mute switch works perfectly under both iOS8 and iOS7!!!







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Dec 9 '14 at 18:47









                                          slamorslamor

                                          2,98521012




                                          2,98521012













                                          • Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.

                                            – Ganee....
                                            Jul 30 '15 at 7:31



















                                          • Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.

                                            – Ganee....
                                            Jul 30 '15 at 7:31

















                                          Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.

                                          – Ganee....
                                          Jul 30 '15 at 7:31





                                          Could you please help me, I have voip call feature in my app, I have to play music/ring tone when user gets a call. I want to play this ringtone even if app is in the background mode. If I use "AVAudioSessionCategoryPlayAndRecord" it is not working with mute switch, if I use "AVAudioSessionCategoryAmbient", it is not playing in the background. Please give me suggestions to achieve both cases.

                                          – Ganee....
                                          Jul 30 '15 at 7:31











                                          2















                                          For Swift




                                          Below framework works perfectly in device



                                          https://github.com/akramhussein/Mute



                                          Just install using pod or download from Git



                                          pod 'Mute'


                                          and use like below code



                                          import UIKit
                                          import Mute

                                          class ViewController: UIViewController {

                                          @IBOutlet weak var label: UILabel! {
                                          didSet {
                                          self.label.text = ""
                                          }
                                          }

                                          override func viewDidLoad() {
                                          super.viewDidLoad()

                                          // Notify every 2 seconds
                                          Mute.shared.checkInterval = 2.0

                                          // Always notify on interval
                                          Mute.shared.alwaysNotify = true

                                          // Update label when notification received
                                          Mute.shared.notify = { m in
                                          self.label.text = m ? "Muted" : "Not Muted"
                                          }

                                          // Stop after 5 seconds
                                          DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                                          Mute.shared.isPaused = true
                                          }

                                          // Re-start after 10 seconds
                                          DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
                                          Mute.shared.isPaused = false
                                          }
                                          }

                                          }





                                          share|improve this answer




























                                            2















                                            For Swift




                                            Below framework works perfectly in device



                                            https://github.com/akramhussein/Mute



                                            Just install using pod or download from Git



                                            pod 'Mute'


                                            and use like below code



                                            import UIKit
                                            import Mute

                                            class ViewController: UIViewController {

                                            @IBOutlet weak var label: UILabel! {
                                            didSet {
                                            self.label.text = ""
                                            }
                                            }

                                            override func viewDidLoad() {
                                            super.viewDidLoad()

                                            // Notify every 2 seconds
                                            Mute.shared.checkInterval = 2.0

                                            // Always notify on interval
                                            Mute.shared.alwaysNotify = true

                                            // Update label when notification received
                                            Mute.shared.notify = { m in
                                            self.label.text = m ? "Muted" : "Not Muted"
                                            }

                                            // Stop after 5 seconds
                                            DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                                            Mute.shared.isPaused = true
                                            }

                                            // Re-start after 10 seconds
                                            DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
                                            Mute.shared.isPaused = false
                                            }
                                            }

                                            }





                                            share|improve this answer


























                                              2












                                              2








                                              2








                                              For Swift




                                              Below framework works perfectly in device



                                              https://github.com/akramhussein/Mute



                                              Just install using pod or download from Git



                                              pod 'Mute'


                                              and use like below code



                                              import UIKit
                                              import Mute

                                              class ViewController: UIViewController {

                                              @IBOutlet weak var label: UILabel! {
                                              didSet {
                                              self.label.text = ""
                                              }
                                              }

                                              override func viewDidLoad() {
                                              super.viewDidLoad()

                                              // Notify every 2 seconds
                                              Mute.shared.checkInterval = 2.0

                                              // Always notify on interval
                                              Mute.shared.alwaysNotify = true

                                              // Update label when notification received
                                              Mute.shared.notify = { m in
                                              self.label.text = m ? "Muted" : "Not Muted"
                                              }

                                              // Stop after 5 seconds
                                              DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                                              Mute.shared.isPaused = true
                                              }

                                              // Re-start after 10 seconds
                                              DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
                                              Mute.shared.isPaused = false
                                              }
                                              }

                                              }





                                              share|improve this answer














                                              For Swift




                                              Below framework works perfectly in device



                                              https://github.com/akramhussein/Mute



                                              Just install using pod or download from Git



                                              pod 'Mute'


                                              and use like below code



                                              import UIKit
                                              import Mute

                                              class ViewController: UIViewController {

                                              @IBOutlet weak var label: UILabel! {
                                              didSet {
                                              self.label.text = ""
                                              }
                                              }

                                              override func viewDidLoad() {
                                              super.viewDidLoad()

                                              // Notify every 2 seconds
                                              Mute.shared.checkInterval = 2.0

                                              // Always notify on interval
                                              Mute.shared.alwaysNotify = true

                                              // Update label when notification received
                                              Mute.shared.notify = { m in
                                              self.label.text = m ? "Muted" : "Not Muted"
                                              }

                                              // Stop after 5 seconds
                                              DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
                                              Mute.shared.isPaused = true
                                              }

                                              // Re-start after 10 seconds
                                              DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) {
                                              Mute.shared.isPaused = false
                                              }
                                              }

                                              }






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Mar 29 '18 at 9:12









                                              Govind PrajapatiGovind Prajapati

                                              842523




                                              842523























                                                  1














                                                  Here two examples how to use AudioSessionInitialize:
                                                  http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/






                                                  share|improve this answer
























                                                  • Link is broken.

                                                    – Daniel Springer
                                                    Nov 6 '18 at 22:24
















                                                  1














                                                  Here two examples how to use AudioSessionInitialize:
                                                  http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/






                                                  share|improve this answer
























                                                  • Link is broken.

                                                    – Daniel Springer
                                                    Nov 6 '18 at 22:24














                                                  1












                                                  1








                                                  1







                                                  Here two examples how to use AudioSessionInitialize:
                                                  http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/






                                                  share|improve this answer













                                                  Here two examples how to use AudioSessionInitialize:
                                                  http://www.restoroot.com/Blog/2008/12/25/audiosessioninitialize-workarounds/







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Dec 25 '08 at 19:55









                                                  catlancatlan

                                                  19.2k85666




                                                  19.2k85666













                                                  • Link is broken.

                                                    – Daniel Springer
                                                    Nov 6 '18 at 22:24



















                                                  • Link is broken.

                                                    – Daniel Springer
                                                    Nov 6 '18 at 22:24

















                                                  Link is broken.

                                                  – Daniel Springer
                                                  Nov 6 '18 at 22:24





                                                  Link is broken.

                                                  – Daniel Springer
                                                  Nov 6 '18 at 22:24


















                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f287543%2fhow-to-programmatically-sense-the-iphone-mute-switch%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Contact image not getting when fetch all contact list from iPhone by CNContact

                                                  count number of partitions of a set with n elements into k subsets

                                                  A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks