I'm currently working on a small project, the meaning of the project/app is that if you were to shake the device the flash ignites. If you shake it again the light turns off!
The first 3/4 toggles between light on or off are working ok, but after 3/4 toggles it is imposible to turn of the light since the device doesnt detect a shake.
Another small problem (since iOS 5.0) is that if a motion is detected the flash will blink very short (1 sec) and than power on. This looks like a short flash.
What am i doing wrong
Detecting the shake:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
NSLog(#"shaken, not stirred.");
[self playSound];
[self toggleFlashlight];
}
}
Toggling the Flash ligt:
- (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];
}
}
}
I would really appreciate your expertise in solving the problem for me!
Well, I don't know much about it, but it's possible you're starting/stopping the session too quickly (calling ToggleFlashLight too soon after last calling it). You might see this code here... It suggests that you do all that (except for the Torch/FlashModeOn) once to create the session, and then just do the lock/torchModeOn/flashModeOn/unlock to turn on and lock/torchModeOff/flashModeOff/unlock to turn off.
Calling your code in
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
instead of motionEnded might solve your problems.
Related
how can i develop or is there any way to achieve the flashlight flash on the beat of the music?
i have search over google for the functionality but won't found anything that will draw me the exact way so i would like to have some expert advice that if anyone had done that before or have an idea how to do it?
looking for something like this video
i know how to turn on and off the flash light here is the code but don't know how to handle that on beat of music
-(void) turnTorchOn: (bool) on
{
Class captureDeviceClass = NSClassFromString(#"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (on) {
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
torchIsOn = YES;
} else {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
torchIsOn = NO;
}
[device unlockForConfiguration];
}
}
}
please help thanks in advance :)
Having no knowledge whatsoever regarding Audio-technology I have stumbled upon a tutorial that should help you out pretty easily. The tutorial is for a music visualizer however the foundation is alike.
http://www.raywenderlich.com/36475/how-to-make-a-music-visualizer-in-ios
I want to turn on torch mode AVCaptureTorchModeOn in my app while doing video recording.
I m using below code.
-(void)set_TorchMode:(BOOL)turnOn
{
AVCaptureDevice *theDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([theDevice hasTorch]) {
[theDevice lockForConfiguration: nil];
AVCaptureTorchMode currentMode = [theDevice torchMode];
BOOL isAlreadyTurnedOn = (AVCaptureTorchModeOn == currentMode);
if (isAlreadyTurnedOn != turnOn) {
[theDevice setTorchMode: turnOn? AVCaptureTorchModeOn: AVCaptureTorchModeOff];
}
[theDevice unlockForConfiguration];
}
}
I m calling this method while start recording to turn ON and while stop recording to turn it OFF.
Its working fine for me first time when i record, but while start recording second time, its turn on but immediately turns OFF.Its not keeping ON while recording is running.
Thanks for any help.
Following code is implemented for the turn on and off back light .
May this helping to you,
- (void) setTorchOn:(BOOL)isOn
{
AVCaptureDevice* device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil]; //you must lock before setting torch mode
[device setTorchMode:isOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff];
[device unlockForConfiguration];
}
- (IBAction)changedState:(id)sender {
UISwitch *switchValue = (UISwitch*)sender;
[self setTorchOn:[switchValue isOn]];
}
please test this code into the devices.
I'm playing around with a simple little flashlight app that turns on and off the LED flash when you press buttons on my view.
It works just fine, but when I turn off the flash, it blinks once before turning off. Any ideas what's causing this behavior?
Here's the pertinent code:
//
// No_Frills_FlashlightViewController.m
// No Frills Flashlight
//
// Created by Terry Donaghe on 8/9/11.
// Copyright 2011 Tilde Projects. All rights reserved.
//
#import "No_Frills_FlashlightViewController.h"
#implementation No_Frills_FlashlightViewController
#synthesize AVSession;
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)TurnOnLight:(id)sender {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVSession = [[AVCaptureSession alloc] init];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
[AVSession addInput:input];
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
[AVSession addOutput:output];
[AVSession beginConfiguration];
[device lockForConfiguration:nil];
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
[device unlockForConfiguration];
[AVSession commitConfiguration];
[AVSession startRunning];
[self setAVSession:AVSession];
[output release];
}
- (IBAction)TurnOffLight:(id)sender {
[AVSession stopRunning];
[AVSession release];
AVSession = nil;
}
- (IBAction)DoNothing:(id)sender {
}
#end
AVSession is just a class level AVCaptureSession variable.
And yes, this is code I just found on the internets. I'm just playing and trying to figure things out.
I figured out what was going on, and it had nothing to do with the code. :) ID10T error.
I had copied the "Turn On" button to create the "Turn Off" button. I forgot to unwire the "Turn Off" button's connection to the TurnOnLight method that was there due to the copying.
I simply removed that connection and now the app works perfectly! :)
Lesson Learned: Sometimes it's not your source code that's the problem. :D
I'm currently using the below code to turn on and off my iPhone 4 LED light and it's working great, but the only problem is that every time I turn the LED on there is a slight delay. However it turns off instantly. I need it to fire instantly to implement a strobe like feature, and because it's just more convenient.
I've noticed that in Apple's camera app and many other apps that the LED turns on and off instantaneously when you hit the power button.
I've tried adding some of the objects like "session" and "device" as instance variables to my view controller in order to have the iPhone create those objects at load time, however I haven't had any luck in getting it to work.
I've also tried looking at apples WWDC sample code but I just can't seem to decipher their complex code. Can someone please help me figure this out i've been trying for about 4 days to get this to work.
.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface FlashlightViewController : UIViewController {
AVCaptureSession *torchSession;
}
#property (nonatomic, retain) AVCaptureSession * torchSession;
- (void) toggleTorch;
#end
.m
#import "FlashlightViewController.h"
#implementation FlashlightViewController
#synthesize torchSession;
- (void)dealloc
{
[torchSession release];
[super dealloc];
}
- (void)viewDidLoad
{
[self toggleTorch];
[super viewDidLoad];
}
- (void) toggleTorch
{
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 setTorchSession:session];
[session release];
}
else {
NSLog(#"It's currently on.. turning off now.");
[torchSession stopRunning];
}
}
}
Do everything (all the session and device configuration stuff) except the flash configuration block before you want to turn the flash LED on, during app init or view load.
Then just set torch mode on when you want to turn the LED on. Something like:
[self.myDevice lockForConfiguration:nil];
[self.myDevice setTorchMode:AVCaptureTorchModeOn];
[self.myDevice setFlashMode:AVCaptureFlashModeOn];
[self.myDevice unlockForConfiguration];
Make sure that myDevice is a properly configured property during your init.
A bit necromantic, but here is a great Library to do it :
LARSTTorch
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Turn on torch/flash on iPhone 4
I just want to be able to turn on the led light. Is there a simple way to do this, or am I going to need to, say, set up the phone to take a video, simulate it videoing with the light on, but not save the video? Something like that? Thanks.
Try this, it worked fine for me.
#import <AVFoundation/AVFoundation.h>
- (void) turnTorchOn: (bool) on {
Class captureDeviceClass = NSClassFromString(#"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (on) {
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
torchIsOn = YES;
} else {
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
torchIsOn = NO;
}
[device unlockForConfiguration];
}
}
}