iPhone 检测按下音量键。
我需要检测用户何时按下硬件音量键,(应用程序商店安全方法)我尝试了很多方法,但没有成功。你知道如何实现这样的功能吗?目前我正在注册通知,但他们似乎没有接到电话。这是我的代码:
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");}
任何提示将不胜感激。
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];
And the receiver method is:
-(void)volumeChanged:(NSNotification *)notification{
NSLog(@"YAY, VOLUME WAS CHANGED");}
Any tips would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在通知触发之前启动音频会话:
现在您可以订阅通知:
要获取音量:
您需要存储音量并将其与从通知中获得的先前值进行比较,以了解哪个按钮被按下按下。
当用户按下音量键时,该解决方案仍然会调整系统音量,并显示音量叠加层。如果您想避免更改系统音量并显示叠加层(本质上完全重新调整音量键的用途),请参阅此答案
You need to start an audio session before the notification will fire:
Now you can subscribe to the notification:
To get the volume:
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