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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top