iPhone检测音量键preSS。 [英] iPhone Detect Volume Keys press.

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

问题描述

我需要在用户presses硬件音量键,(App Store的安全的方法),我已经尝试了一些东西,没有运气来检测。你知道如何实现这样的功能?在present我注册的通知,但他们似乎并没有被调用。这里是我的code:

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

任何提示将不胜AP preciated。

Any tips would be greatly appreciated.

推荐答案

您需要通知之前启动一个音频会话将火:

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

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

现在,您可以订阅通知:

Now you can subscribe to the notification:

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

要获得音量:

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

您将需要存储卷,并将其与你的通知才知道哪个按钮是pressed的previous值。

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.

这个解决方案仍然会调节系统音量,当用户presses音量键,并显示音量覆盖。如果你想避免改变系统的体积和显示覆盖(在本质上完全重新调整音量键),看到这个答案

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检测音量键preSS。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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