iPhone 检测音量键按下. [英] iPhone Detect Volume Keys press.

查看:31
本文介绍了iPhone 检测音量键按下.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to detect when the user presses the hardware volume keys, (App Store safe approach) I have tried a number of things with no luck. Do you know how to implement such functionality? At present I am registering for notifications, however they don't seem to get called. Here's my code:

  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");}

任何提示将不胜感激.

推荐答案

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

You need to start an audio session before the notification will fire:

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

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

You will need to store the volume and compare it to the previous value you got from a notification to know which button was pressed.

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

This solution will still adjust the system volume when the user presses the volume key, and show the volume overlay. If you want to avoid changing the system volume and showing the overlay (in essence completely repurpose the volume keys), see this answer

这篇关于iPhone 检测音量键按下.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆