繁体   English   中英

iPhone检测音量键按。

[英]iPhone Detect Volume Keys press.

我需要检测用户何时按下硬件音量键,(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");}

任何提示将非常感谢。

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

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];

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

当用户按下音量键时,此解决方案仍将调整系统音量,并显示音量叠加。 如果您想避免更改系统音量并显示叠加层(实际上完全重新调整音量键的用途), 请参阅此答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM