开发者

Check if iPad is in silent mode [duplicate]

This question already has answers here: 开发者_高级运维 Closed 11 years ago.

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.

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" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}

Any ideas how to modify it to work universally?

Thanks

Dan.


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 http://blog.stormyprods.com/2008/09/proper-usage-of-mpvolumeview-class.html 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 
      selector:@selector(volumeChanged:) 
      name:@"AVSystemController_SystemVolumeDidChangeNotification" 
      object:nil];
}
- (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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜