Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set AVCaptureTorchModeOn

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.

like image 299
Nikunj Avatar asked Jun 07 '12 12:06

Nikunj


People also ask

What is AVCaptureSession?

An object that configures capture behavior and coordinates the flow of data from input devices to capture outputs.

What is AVF audio?

AVFoundation is Apple's advanced framework for working with time-based media, such as audio and video. This course covers the essentials to using the AVFoundation framework to create audio based apps.


1 Answers

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.

like image 121
Nimit Parekh Avatar answered Oct 08 '22 12:10

Nimit Parekh