sami54
sami54

Reputation: 143

turn on flashlight on Windows 10

My issue is quite simple.

I want to turn the flash On (and keep it On) on a Windows 10 universal app project but nothing I try works.

This is the code

MediaCapture MyMediaCapture = new MediaCapture();

var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
DeviceInformation cameraDevice =
        allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null &&
        x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
cameraDevice = cameraDevice ?? allVideoDevices.FirstOrDefault();

if (cameraDevice == null)
{
    Debug.WriteLine("No camera device found!");
}
else
{
    await MyMediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
    {
        VideoDeviceId = cameraDevice.Id
    });

    var MyVideoDeviceController = MyMediaCapture.VideoDeviceController;
    var MyTorch = MyVideoDeviceController.TorchControl;

    if (MyTorch.Supported)
    {
      var captureElement = new CaptureElement();
      captureElement.Source = MyMediaCapture;
      await MyMediaCapture.StartPreviewAsync();

       FileStream tmp = new FileStream(System.IO.Path.GetTempFileName() + Guid.NewGuid().ToString() + ".mp4", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 10000, FileOptions.RandomAccess | FileOptions.DeleteOnClose);

       var videoFile = await KnownFolders.VideosLibrary.CreateFileAsync(tmp.Name, CreationCollisionOption.GenerateUniqueName);

       var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Wvga);

       await MyMediaCapture.StartRecordToStorageFileAsync(encodingProfile, videoFile);

       MyTorch.PowerPercent = 100;
       MyTorch.Enabled = true;
    }
}

Edit: add code

Upvotes: 2

Views: 3017

Answers (2)

Mike
Mike

Reputation: 2260

You're on the right path. Depending on the device (because of driver-specific implementations), you'll have to start the preview or maybe even start a video recording session for the light to turn on.

Because of that, and to guarantee compatibility with most devices, I'd recommend you actually do both.

Upvotes: 0

James Croft
James Croft

Reputation: 1680

It looks like you're trying to use an old method of accessing the flashlight which we no longer have to use in Windows 10 UWP development. Take a look at the new Lamp feature in Windows.Devices.Lights in this sample on GitHub.

It's a great starting point for using the flash independent of access the camera APIs.

Upvotes: 2

Related Questions