views:

2763

answers:

11

I know that the only way to turn on the flash and keep it on on iPhone 4 is by turning the video camera on. I'm not too sure of the code though. Here is what I am trying:

-(IBAction)turnTorchOn {
AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];

if (videoInput) {
    [captureSession addInput:videoInput];



    AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    [videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()];

    [captureSession addOutput:videoOutput];

    [captureSession startRunning];

    videoCaptureDevice.torchMode = AVCaptureTorchModeOn;
}   

}

Does anybody know if this would work or am I missing anything? (I don't have an iPhone 4 yet to test on -just trying out some of the new API's).

Thanks

A: 

I'd like to know how to get the light on as well.

I know you need to lockForConfiguration:(NSError **)error

but I don't know how.

Cocoa Dev
+2  A: 

the lockforConfiguracion is set in your code, where you declare your AVCaptureDevice is a property.

[videoCaptureDevice lockForConfiguration:nil];

Gustavo Barrientos
A: 

I don't have iphone4 and have not checked the following code, but could you try it?

-(IBAction)turnTorchOn { AVCaptureSession *captureSession = [[AVCaptureSession alloc] init]; AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error = nil; AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];

if (videoInput) { [captureSession addInput:videoInput];

AVCaptureVideoDataOutput* videoOutput = [[AVCaptureVideoDataOutput alloc] init];
[videoOutput setSampleBufferDelegate:self queue:dispatch_get_current_queue()];

[captureSession addOutput:videoOutput];

[captureSession startRunning];

//************

[videoCaptureDevice lockForConfiguration:&error];

[videoCaptureDevice setTorchMode:AVCaptureTorchModeOn];

//************

}

toshiss
toshiss, see my answer above and it will work for you.
iWasRobbed
+5  A: 

Just wanted to post a corrected answer on here, since a different answer on another post froze the app when trying to turn off the torch and it was bugging me. Hopefully others will find this example useful. Just so you know, I have an iPhone 4 and this has been tested and verified to work.

Kudos to the many people around here that left enough breadcrumbs to figure this all out.

First, in your .h file:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface FlashlightViewController : UIViewController {

    AVCaptureSession *torchSession;

}

@property (nonatomic, retain) AVCaptureSession * torchSession;

- (void) toggleTorch;

@end

Then in your .m file:

#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];

    }

}

}

Since mine is in a navigation view, I use this to toggle it back off (you can place this anywhere, in a button, etc.)

- (void)viewWillDisappear:(BOOL)animated {

    [self toggleTorch];

}
iWasRobbed
A: 

I implemented this and it works great to turn the torch on, but then my application stops responding to events to turn it off.

Next I tried putting the code that turns it on inside it's thread. That allowed my UI to respond, but now calling [torchSession stopRunning] doesn't do anything.

Any ideas?

UPDATE: All I want to do is toggle the light on and off, so I commented out a bunch of the code above and now it works great. Here's what I've got:

if (light.on) 
{               
    NSLog(@"It's currently off.. turning on now.");

    AVCaptureSession *session = [[AVCaptureSession alloc] init];

    [session beginConfiguration];
    [device lockForConfiguration:nil];

    [device setTorchMode:AVCaptureTorchModeOn];

    [device unlockForConfiguration];

    [session commitConfiguration];
    [session startRunning];

    [self setTorchSession:session];
    [session release];

}
else 
{

    NSLog(@"It's currently on.. turning off now.");

    [torchSession stopRunning];
    [torchSession beginConfiguration];
    [device lockForConfiguration:nil];

    [device setTorchMode:AVCaptureTorchModeOff];

    [device unlockForConfiguration];
    [torchSession commitConfiguration];
    [torchSession startRunning];            
    [torchSession stopRunning];

}
David Rea
See my post below: http://stackoverflow.com/questions/3190034/turn-on-torch-flash-on-iphone-4/3367424#3367424
iWasRobbed
A: 

Can you post the full code to work from? I am trying to decipher from bits and pieces of various code and it's not working for me. If I can read and modify working code, hopefully I can wrap my brain around it better. Much thanks in advance.

LND
A: 

Either that, or if the code can be commented, that'd help a great deal for learning what's going on. Either way, this is all great stuff to learn from. Thanks to everyone contributing!

Chaia
A: 

Hey,

Thank you for providing the solution where you can use the rear LED with music support, I was looking for such a solution for a long time. I tried the solution but there was a lag when switching the light back on i.e. there is a 1 sec delay when the light is switched on. Is there a way to minimize that delay.

Ankur
A: 

@Cocoa Dev As detailed above, you must use AVFoundation.framework. Look above to see the code.

Magician Software
A: 

So I have noobish question, how do I activate this code (the code IWasRobbed posted) when the application launches? watching the debugger when I run it on my iphone 4, the NSlog messages never show up, so I'm not sure the code is getting executed.

BigB
BigB... please post this as a new question, not as an additional answer. I'll give you a hint though, try calling `[self toggleTorch];` in your `viewDidLoad` method of the first view that you load upon launch
iWasRobbed
A: 

I can't make sense of this. It wont turn off unless I close the app and then it wont turn back on. Can you post the entire thing David?

Thanks

chadrico