检查iPad是否处于静音模式

Dan*_*Dan 11 iphone audio objective-c ipad ios

可能重复:
在iOS5中检测静音模式?

我已经使用下面的代码检查静音模式是否打开,它在iPhone上按预期工作,但在iPad上,无论如何都会返回扬声器.

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];
}
Run Code Online (Sandbox Code Playgroud)

任何想法如何修改它普遍工作?

谢谢

担.

Gab*_*iel 0

在 XIB 中,您可以添加一个滑块来检查音量级别,因此基本上您可以判断它是否静音,并了解音量级别。为了更多地了解这个类,这里​​是链接http://blog.stormyprods.com/2008/09/proper-usage-of-mpvolumeview-class.html,但首先尝试一下:

下面的代码将创建类似音量条的东西。

- (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]];
}
Run Code Online (Sandbox Code Playgroud)

我听说,出于某种原因,如果您使用某个类别(我的示例中的类别),苹果不允许您销售应用程序,但我对此不太确定,我会仔细检查并确保您是“允许”使用它。但代码应该可以工作。