如何实现iPhone的音量键快门? [英] How to implement a volume key shutter for iPhone?
问题描述
我想使用 iOS5
的本地摄像头实现相同的行为:
- 按音量+按钮拍照
存档的理想方法是什么?
有没有办法捕获音量按键事件?
googling&搜索几个小时,我发现一个解决方案:使用 NSNotificationCenter
:
...
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(volumeChanged :)
name:@AVSystemController_SystemVolumeDidChangeNotification
object:nil];
...
- (void)volumeChanged:(NSNotification *)notification {
[self takePhoto];
}
但是,它有两个问题:
- 每当按下音量键时,都会显示当前系统音量的半透明叠加层,这不是我想要的。
- 对于本机摄像机,当按音量键作为快门时,系统音量不会改变,但是使用上述方法,系统音量将发生变化。 >
我发现了另一种方法来隐藏系统卷覆盖和绕过系统卷当音量键按自己改变
不好的部分:这是一个超级 UGLY 黑客。
然而,很好的部分是:这个丑陋的黑客使用NO私人API。
另一个注意事项是:它仅适用于ios5 +(无论如何,对于我的问题,因为AVSystemController_SystemVolumeDidChangeNotification仅适用于ios5,所以这个UGLY黑客只适合我的问题。)
它的工作方式:充当音乐/电影播放器应用程序,让调整应用程序音量的音量键。
代码:
//这4行代码告诉系统这个程序需要播放声音/音乐
AVAudioPlayer * p = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@photo-shutter.wav]] error:NULL];
[p prepareToPlay];
[p stop];
[p release];
//这5行代码告诉系统这个窗口里面有一个卷视图,所以没有必要显示一个系统覆盖
[[self.view viewWithTag :54870149] removeFromSuperview];
MPVolumeView * vv = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100,-100,100,100)];
[self.view addSubview:vv];
vv.tag = 54870149;
[vv release];
(发现这个超丑的方法花了5个小时... shit ...草尼马啊另一件事:
如果你采取上述的黑客,你需要运行代码,每当你的应用程序成为活跃的时候。
所以,你可能需要把一些代码放在你的应用程序委托中。
- (void)applicationDidBecomeActive :((UIApplication *)申请
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.
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:
// these 4 lines of code tell the system that "this app needs to play sound/music"
AVAudioPlayer* p = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"photo-shutter.wav"]] error:NULL];
[p prepareToPlay];
[p stop];
[p release];
// these 5 lines of code tell the system that "this window has an volume view inside it, so there is no need to show a system overlay"
[[self.view viewWithTag:54870149] removeFromSuperview];
MPVolumeView* vv = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, -100, 100, 100)];
[self.view addSubview:vv];
vv.tag = 54870149;
[vv release];
(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.
- (void)applicationDidBecomeActive:(UIApplication *)application
这篇关于如何实现iPhone的音量键快门?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!