【问题标题】:iPhone Detect Volume Keys press.iPhone 检测音量键按下。
【发布时间】:2011-11-23 14:44:27
【问题描述】:

我需要检测用户何时按下硬件音量键,(App Store 安全方法)我尝试了很多没有运气的事情。你知道如何实现这样的功能吗?目前我正在注册通知,但他们似乎没有被调用。这是我的代码:

  AudioSessionInitialize(NULL, NULL, NULL, NULL);
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
                       selector:@selector(volumeChanged:) 
                           name:@"AVSystemController_SystemVolumeDidChangeNotification" 
                         object:nil];

而接收方法是:

-(void)volumeChanged:(NSNotification *)notification{
         NSLog(@"YAY, VOLUME WAS CHANGED");}

任何提示将不胜感激。

【问题讨论】:

标签: iphone ios audio avfoundation volume


【解决方案1】:

您需要在通知触发之前启动音频会话:

AudioSessionInitialize(NULL, NULL, NULL, NULL); 
AudioSessionSetActive(true); 

现在您可以订阅通知了:

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(volumeChanged:) 
    name:@"AVSystemController_SystemVolumeDidChangeNotification" 
    object:nil];

获取音量:

float volume = [[[notification userInfo] 
    objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] 
    floatValue];

您需要存储音量并将其与您从通知中获得的先前值进行比较,以了解按下了哪个按钮。

当用户按下音量键时,此解决方案仍会调整系统音量,并显示音量叠加。如果您想避免更改系统音量并显示覆盖(本质上完全重新调整音量键),see this answer

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 2021-12-03
    • 2012-05-26
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多