我目前正在做一个小项目,该项目/应用程序的含义是,如果您要摇动设备,闪光灯就会点燃。如果你再次摇晃它,灯就会熄灭!
开灯或关灯之间的前 3/4 切换工作正常,但在 3/4 切换后,由于设备未检测到抖动,因此无法关闭灯。
另一个小问题(自 iOS 5.0 起)是,如果检测到动作,闪光灯会闪烁很短的时间(1 秒)然后开机。这看起来像一个短暂的闪光。
我究竟做错了什么
检测抖动:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
NSLog(@"shaken, not stirred.");
[self playSound];
[self toggleFlashlight];
}
}
切换闪光灯:
- (void)toggleFlashlight
{
AVCaptureDevice *device =
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
if (device.torchMode == AVCaptureTorchModeOff) {
NSLog(@"It's currently off.. turning on now.");
AVCaptureDeviceInput *flashInput =
[AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
AVCaptureVideoDataOutput *output =
[[AVCaptureVideoDataOutput alloc] init];
AVCaptureSession *session =
[[AVCaptureSession alloc] init];
[session beginConfiguration];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
[session addInput:flashInput];
[session addOutput:output];
[device unlockForConfiguration];
[output release];
[session commitConfiguration];
[session startRunning];
[self setAVSession:session];
[session release];
}
else {
NSLog(@"It's currently on.. turning off now.");
[AVSession stopRunning];
}
}
}
我非常感谢您为我解决问题的专业知识!