Check if iPad is in silent mode

Possible Duplicate:
Detect Silent mode in iOS5?

i have used the code below to check if silent mode is on, it works as expected on the iPhone but on the iPad it returns speaker regardless.

  • Add values in NSMutableDictionary in iOS with Objective-C
  • Cannot subscript a value of type '?' with an index of type 'String'
  • Present storyboard ViewController from another ViewController
  • Gesture Recognizer Not Called
  • In iOS, what's the difference between autoresizing, AutoLayout, and constraints?
  • What are the limitations of HTML5 audio on Android and iOS?
  • CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
    if (CFStringGetLength(state) == 0) { 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Silent mode" 
                                                        message:@"Please turn sound on"
                                                       delegate:self cancelButtonTitle:@"Ok" 
        [alert show];
        [alert release];

    Any ideas how to modify it to work universally?



    2 Solutions Collect From Internet About “Check if iPad is in silent mode”

    In your XIB you can add a slider to check what the volume level is at, so basically you can tell if it is silent, and know the level of the volume. For more understanding of this class, here’s the link but try this first:

    The following code will create something like a volume bar.

    - (void)viewDidLoad {
            // create a frame for MPVolumeView image
     CGRect frame = volumeViewHolder.bounds; // CGRectMake(0, 5, 180, 0);
     volumeView = [[[MPVolumeView alloc] initWithFrame:frame] autorelease];
     [volumeView sizeToFit];
     [volumeViewHolder addSubview:volumeView];
     for (UIView *view in [volumeView subviews]){
      if ([[[view class] description] isEqualToString:@"MPVolumeSlider"]) {
       volumeViewSlider = view;
     [[NSNotificationCenter defaultCenter] addObserver:self 
    - (void) volumeChanged:(NSNotification *)notify
    [volumeViewSlider setValue:[[[notify userInfo] objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue]];

    I heard that for some reason apple doesn’t allow you to sell an app if you use a certain class (the one in my example) but I’m not too sure about this, I would double-check and make sure that you are ‘allowed’ to use it. But the code should work.

    This SO answer answers it nicely:

    Detect Silent mode in iOS5?

    And regarding Gabe’s answer, if his answer does indeed use a private API, Apple will reject your app.