我想用iOS5
的本机摄像机实现同样的行为
,归档它的理想方法是什么?是否有任何方法来捕获按下的卷键事件?
在搜索了几个小时之后,我找到了一个解决方案:使用NSNotificationCenter
:
...
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
...
- (void)volumeChanged:(NSNotification *)notification{
[self takePhoto];
}
然而,它有两个问题:
发布于 2011-12-06 16:42:00
我已经找到了另一种方法来隐藏“系统卷覆盖”和“绕过系统卷更改时按下卷键”。
最糟糕的是:这是一个超级丑陋的黑客。
然而,的好处是:这个丑陋的黑客不使用私有API.。
另一个注意事项是:它只适用于ios5+ (无论如何,对于我的问题,因为ios5+只适用于ios5,所以这个丑陋的攻击正好适合我的问题)。
它的工作方式:“扮演一个音乐/电影播放器应用程序,让音量键来调整应用-音量”。
代码:
// 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个小时来发现这个超丑的方法.妈的..。草尼马啊!)
另一件事:如果你采取上述黑客,你需要每次运行代码时,你的应用程序变得活跃。因此,您可能需要在应用程序委托中添加一些代码。
- (void)applicationDidBecomeActive:(UIApplication *)application
发布于 2012-05-05 10:14:38
构建在huxia代码的基础上,它可以在ios5+上运行,无需每次代码处于活动状态时运行,只需在开始时运行一次即可。
// 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:@"photoshutter.wav"]] error:NULL];
[p prepareToPlay];
[p stop];
//make MPVolumeView Offscreen
CGRect frame = CGRectMake(-1000, -1000, 100, 100);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[volumeView sizeToFit];
[self.view addSubview:volumeView];
发布于 2012-09-10 09:32:40
…
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(volumeChanged:)
name:@"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
…
- (void)volumeChanged:(NSNotification *)notification{
CGRect frame = CGRectMake(-1000, -1000, 100, 100);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[volumeView sizeToFit];
[self.view addSubview:volumeView];
[volumeView release];
[self takePhoto];
}
https://stackoverflow.com/questions/8397170
复制相似问题