在Windows Phone 8.1中切换手电筒

谁能说如何使用C#在Windows Phone 8.1中切换手电筒? 似乎Windows Phone 8.1中有许多API更改,并且不支持WP 8.0中的大多数API。 答案非常感谢。

我可以在我的Lumia 820上使用TorchControl – 首先你必须指定你将使用哪个相机 – 默认是前面(我认为这就是为什么你可能会发现一些问题)而我们想要后面的那个 – 一个带有闪光灯。 示例代码:

// edit - I forgot to show GetCameraID: private static async Task 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; 

请注意,完成后调用captureManager.Dispose()是个好主意。


另请注意,在某些手机上打开手电筒/手电筒时,您需要先开始预览。

Windows Phone 8.1是第一个带有用于控制摄像头灯的专用API的版本。 此API源自Windows 8.1,但可用于Windows Phone 8.1项目和Windows Phone Silverlight 8.1项目。

 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; } 

注意:注意:在WP8.1开发人员预览中,TorchControl.Supported在大多数手机上都返回false。 在WP 8.1发布之前,预计将通过固件更新来修复。 在撰写本文时经过测试的手机:Lumia 620,822,1020:不工作,Lumia 1520:工作。

在诺基亚Lumia 1520中,您使用FlashControl切换闪光灯而不是TorchControl。

  //to switch OFF flash light mediacapture.VideoDeviceController.FlashControl.Enabled = false; //to switch ON flash light mediacapture.VideoDeviceController.FlashControl.Enabled = true; 

不适用于我的Lumia 1520.您需要开始video录制以使手电筒工作:

  var videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga); var videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync("tempVideo.mp4", CreationCollisionOption.GenerateUniqueName); await captureManager.StartRecordToStorageFileAsync(videoEncodingProperties, videoStorageFile); 

在我的Lumia 1520中。我需要开始video录制并开始预览以使手电筒工作:

 await captureManager.StartPreviewAsync();