【问题标题】:Check if iPad is in silent mode [duplicate]检查iPad是否处于静音模式[重复]
【发布时间】:2011-11-24 16:26:16
【问题描述】:

可能重复:
Detect Silent mode in 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];
}

有什么想法可以修改它以使其通用吗?

谢谢

丹。

【问题讨论】:

    标签: iphone objective-c ios ipad audio


    【解决方案1】:

    在您的 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]];
    }
    

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

    【讨论】:

      【解决方案2】:

      这个 SO 答案很好地回答了它:

      Detect Silent mode in iOS5?

      关于 Gabe 的回答,如果他的回答确实使用了私有 API,Apple 将拒绝您的应用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-24
        • 1970-01-01
        • 2012-07-30
        • 1970-01-01
        • 2016-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多