Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn Flash On/Off

OK, My issue is quite simple.

I've managed to turn the flash On (and keep it On).

However, I'm still not sure how to turn it off (lol).

Here's my code :

var sensorLocation = CameraSensorLocation.Back;

try
{
    // get the AudioViceoCaptureDevice
    var avDevice = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
        AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());

    // turn flashlight on
    var supportedCameraModes = AudioVideoCaptureDevice
        .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
    if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
    {
        avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);

        // set flash power to maxinum
        avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower,
            AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
    }
    else
    {
        turnWhiteScreen(true);
    }

}
catch (Exception ex)
{
    // Flashlight isn't supported on this device, instead show a White Screen as the flash light
    turnWhiteScreen(true);
}

Any ideas?


P.S.

  • I imagined that converting .ons to .offs could have worked, but it doesn't.
  • This has been tested on a HTC 8S and Lumia 820.
like image 904
Dr.Kameleon Avatar asked Jun 23 '13 09:06

Dr.Kameleon


People also ask

How do I turn the flashlight off on my smartphone?

Press Go to home screen. You should see the Flashlight on button you just created. Tapping the shortcut will turn the flashlight on, then you can press the button in the Google Assistant screen to turn it off. You can long-press the button to adjust its size and move it around on your home screen as needed.


1 Answers

It looks like you can't retrieve the acquisition device twice (I'm not sure why), so you should store it in a property:

protected AudioVideoCaptureDevice Device { get; set; }

private async void ButtonTurnOn_Click(object sender, RoutedEventArgs e)
{
    var sensorLocation = CameraSensorLocation.Back;

    try
    {
        if (this.Device == null)
        {
            // get the AudioViceoCaptureDevice
            this.Device = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
            AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
        }

        // turn flashlight on
        var supportedCameraModes = AudioVideoCaptureDevice
            .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
        if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
        {
            this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);

            // set flash power to maxinum
            this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower,
                AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
        }
        else
        {
            turnWhiteScreen(true);
        }

    }
    catch (Exception ex)
    {
        // Flashlight isn't supported on this device, instead show a White Screen as the flash light
        turnWhiteScreen(true);
    }
}

Then, to turn it off:

private void ButtonTurnOff_Click(object sender, RoutedEventArgs e)
{
    var sensorLocation = CameraSensorLocation.Back;

    try
    {
        // turn flashlight on
        var supportedCameraModes = AudioVideoCaptureDevice
            .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
        if (this.Device != null && supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.Off))
        {
            this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
        }
        else
        {
            turnWhiteScreen(false);
        }
    }
    catch (Exception ex)
    {
        // Flashlight isn't supported on this device, instead show a White Screen as the flash light
        turnWhiteScreen(false);
    }
}
like image 190
Kevin Gosse Avatar answered Sep 20 '22 01:09

Kevin Gosse