检测iPhone音量按钮按下? [英] Detect iPhone Volume Button Up Press?

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

问题描述

是否有可以收听的通知告诉我iPhone的音量何时变为上升

Is there a notification that I can listen to that will tell me when an iPhone's volume is turned up?

我知道 AVSystemController_SystemVolumeDidChangeNotification ,但只有在音量调整时才会触发通知

I know about the AVSystemController_SystemVolumeDidChangeNotification, but it is essential that the notification only be triggered when the volume has been turned up, not up or down.

其次,如何隐藏按下音量增大按钮时出现的半透明视图,显示系统的音量? Camera + 已经实现了这一点。

Secondly, how can I hide the translucent view that appears when the volume up button is pressed, showing the system's volume? Camera+ has implemented this.

推荐答案

没有记录的方法,但你可以使用这个解决方法。注册 AVSystemController_SystemVolumeDidChangeNotification 通知并添加 MPVolumeView ,这将阻止系统卷视图显示。

There is no documented way to to this, but you can use this workaround. Register for AVSystemController_SystemVolumeDidChangeNotification notification and add an MPVolumeView which will prevent the system volume view from showing up.

MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, 0, 10, 0)];
[volumeView sizeToFit];
[self.view addSubview:volumeView];

不要忘记开始音频会话

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

在这种情况下, MPVolumeView 被隐藏来自用户。

In This case, the MPVolumeView is hidden from the user.

至于检查是否按下了音量增大或减小,只需获取当前应用程序的音量

As for checking if volume up or down was pressed, just grab the current application's volume

float volumeLevel = [[MPMusicPlayerController applicationMusicPlayer] volume];  

并在通知回调中按下按钮后将其与新音量进行比较

and compare it with new volume after the button was pressed in notification callback

如果您不想自己动手,可以在github上找到一个免费课程

If you don't want to do it by yourself, there's a drop-in class available in github

https://github.com/blladnar/RBVolumeButtons

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

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