如何为iPhone实现音量键快门?
我想用iOS5
的原生相机实现相同的行为:
- 按音量+按钮拍照
理想的存档方式是什么? 有没有办法捕获音量键按下事件
?搜索了几个小时,我找到了 1 个解决方案:使用 NSNotificationCenter
:
...
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
...
- (void)volumeChanged:(NSNotification *)notification{
[self takePhoto];
}
但是,它有 2 个问题:
- 每次按音量键时都会显示“当前系统音量”的半透明覆盖层,这不是我想要的。
- 对于原生相机来说,当按音量键作为快门时,系统音量不会改变,但是使用上述方法,系统音量会改变。
I want to implement the same behavior with the native camera of iOS5
:
- press the volume + button to take a photo
What's the ideal way to archive it?
Are there any ways to capture the volume key pressed event?
After googling & searching around for hours, I found 1 solution: using NSNotificationCenter
:
...
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
...
- (void)volumeChanged:(NSNotification *)notification{
[self takePhoto];
}
However, it has 2 issues:
- There is an semi-transparent overlay of "current system volume" show up every time when pressing the volume key, this is not what I wanted.
- For the native camera, when you press the volume key as shutter, the system volume won't change, however, by using the above method, the system volume will change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我自己找到了另一种方法来隐藏“系统音量覆盖”和“按下音量键时绕过系统音量变化”。
不好的部分:这是一个超级丑陋的黑客行为。
但是,好的部分是:这个丑陋的黑客不使用私有API。
另一个注意事项是:它仅适用于ios5+(无论如何,对于我的问题,因为AVSystemController_SystemVolumeDidChangeNotification仅适用于ios5,所以这个丑陋的黑客正好适合我的问题。)
它的工作方式:“充当音乐/电影播放器应用程序,并让音量键调整应用程序音量”。
代码:(
花了5个小时发现这个超级丑陋的方法...狗屎...草尼马啊!)
另一件事:
如果您采用上述技巧,则需要在应用程序激活时每次都运行代码。
因此,您可能需要将一些代码放入您的应用程序委托中。
I've found another way to hide the "system volume overlay" and "bypass the system volume change when the volume key pressed" by myself.
The bad part: this is an super UGLY hack.
However, the good part is: this ugly hack uses NO private APIs.
Another note is: it only works for ios5+ (anyway, for my issue, since the AVSystemController_SystemVolumeDidChangeNotification only works for ios5, so this UGLY hack just fits my issue.)
The way it work: "act as a music/movie player app and let the volume key to adjust the application-volume".
Code:
(5 hours spending on discovering this super ugly method... shit... 草尼马啊!)
Another thing:
if you take the above hack, you need to run the code EVERY-TIME when your app become active.
So, you might need to put some code into your app delegate.
基于huxia的代码构建,适用于ios5+,无需每次激活时都运行代码,只需在开始时运行一次即可。
Building on huxia's code, this works on ios5+, no need to run the code every time it becomes active, just run it once in the beginning.
……
…
…
目前没有官方方法来捕获音量键按下事件。 Apple 的声明是,如果您允许音量按钮显示相机控件,则音量按钮可以与 UIImagePickerController 配合使用。
其他方法,例如监听通知,似乎是不受支持的黑客行为,苹果团队有时对此视而不见。为了防止出现音量 HUD,您可以使用未记录的
UIApplication
方法:我见过的唯一使用说明是:
我不确定是否或为什么您似乎需要禁用 HUD特定类别,然后是一般类别,但如果没有适当的文档,很难弄清楚。
因此:如果您想遵守规则,请使用 UIImagePickerController 及其相机按钮。如果您发现某个应用程序似乎可以在规则之外运行,那么它可能正在使用上述方法。
There's currently no official way to capture the volume key pressed event. Apple's stated line is that the volume button works with the
UIImagePickerController
if you've allowed it to show camera controls.Other approaches, such as listening for the notification, seem to be unsupported hacks that Apple's team are — anecdotally — sometimes turning a blind eye to. To prevent the volume HUD from appearing you can use the undocumented
UIApplication
methods:The only statement of their use I've seen is:
I'm unsure if or why you seemingly need to disable the HUD for a specific category and then in general, but without proper documentation that's difficult to figure out.
So: use
UIImagePickerController
and its camera buttons if you want to be within the rules. If you've found an app that seems to work outside of the rules then it's probably using the methods above.的
viewDidAppear
中调用此方法viewWillDisappear
我在调用中
I call this method from
viewDidAppear
In
viewWillDisappear
in call