Toggle flashlight in Windows Phone 8.1

2019-01-20 00:18发布

Can anyone say how to toggle flashlight in Windows Phone 8.1 using C#? It seems like there are lots of API changes in Windows Phone 8.1 and most of the API's in WP 8.0 are not supported. Answers are highly appreciated.

5条回答
啃猪蹄的小仙女
2楼-- · 2019-01-20 00:54

In my Lumia 1520. I need to start video recording and start preview to get flashlight working:

await captureManager.StartPreviewAsync();
查看更多
叛逆
3楼-- · 2019-01-20 01:07

I'm able to use TorchControl on my Lumia 820 like this - first you have to specify which camera you will use - the default is front (I think that's why you may find some problems) and we want the back one - the one with flash light. Sample code:

// edit - I forgot to show GetCameraID:
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
    DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
        .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);

    if (deviceID != null) return deviceID;
    else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desiredCamera));
}

// init camera
async private void InitCameraBtn_Click(object sender, RoutedEventArgs e)
{
    var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
    captureManager = new MediaCapture();

    await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
        {
            StreamingCaptureMode = StreamingCaptureMode.Video,
            PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
            AudioDeviceId = string.Empty,
            VideoDeviceId = cameraID.Id
        });
}

// then to turn on/off camera
var torch = captureManager.VideoDeviceController.TorchControl;
if (torch.Supported) torch.Enabled = true;

// turn off
if (torch.Supported) torch.Enabled = false;

Note that it's a good idea to call captureManager.Dispose() after you finish with it.


Note also that on some phones to turn on torch/flashlight you will need to start preview first.

查看更多
何必那么认真
4楼-- · 2019-01-20 01:10

In Nokia Lumia 1520, you use FlashControl to toggle the flash light instead of TorchControl.

    //to switch OFF flash light
    mediacapture.VideoDeviceController.FlashControl.Enabled = false;
    //to switch ON flash light
    mediacapture.VideoDeviceController.FlashControl.Enabled = true;
查看更多
一夜七次
5楼-- · 2019-01-20 01:11

Windows Phone 8.1 is the first version with a dedicated API for controlling the camera light. This API stems from Windows 8.1 but is usable in Windows Phone 8.1 projects and in Windows Phone Silverlight 8.1 projects.

var mediaDev = new MediaCapture();
await mediaDev.InitializeAsync();
var videoDev = mediaDev.VideoDeviceController;
var tc = videoDev.TorchControl;
if (tc.Supported)
   {
   if (tc.PowerSupported)
      tc.PowerPercent = 100;
   tc.Enabled = true;
   }

Note: Note: TorchControl.Supported returns false on most phones in WP8.1 developer preview. It is expected to be fixed by a firmware update by the time WP 8.1 is released. Tested Phones at the time of writing: Lumia 620, 822, 1020: not working, Lumia 1520: working.

查看更多
SAY GOODBYE
6楼-- · 2019-01-20 01:12

Doesn't work on my Lumia 1520. You need to start video recording to get flashlight working:

        var videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);

        var videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync("tempVideo.mp4", CreationCollisionOption.GenerateUniqueName);
        await captureManager.StartRecordToStorageFileAsync(videoEncodingProperties, videoStorageFile);
查看更多
登录 后发表回答