如何打开Windows Phone 8.1上的手电筒

时间:2015-07-22 11:43:33

标签: windows-phone-8.1 flashlight

我基于以下方式创建了项目: https://github.com/Microsoft/real-time-filter-demo/tree/master/RealtimeFilterDemoWP

我的问题是如何在WP8.1上启用闪光灯(手电筒) 我应该使用MediaCapture()吗?

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

当我使用它时崩溃

var videoDev = mediaDev.VideoDeviceController;

未处理的异常

  • 如何为此示例项目添加手电筒?

1 个答案:

答案 0 :(得分:2)

您尚未初始化MediaCaptureSettings,因此当您尝试初始化videoController时,会发生异常。您需要初始化设置,让MediaCapture知道您要使用的设备,并设置VideoDeviceController。此外,对于Windows Phone 8.1相机驱动程序,有些要求您开始预览,或者其他要求您开始视频录制以打开闪光灯。这是由于闪光灯与相机设备紧密耦合。

这里有一些通用代码可以为您提供这个想法。 *免责声明,这是未经测试的。最好确保在异步任务方法中调用它,以便在尝试切换Torch Control属性之前确保等待的调用完成。

private async Task InitializeAndToggleTorch()
{
    // Initialize Media Capture and Settings Objects, mediaCapture declared global outside this method 
    mediaCapture = new MediaCapture();
    MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();

    // Grab all available VideoCapture Devices and find rear device (usually has flash)
    DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    DeviceInformation device = devices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);

   // Set Video Device to device with flash obtained from DeviceInformation
   settings.VideoDeviceId = device.Id;
   settings.AudioDeviceId = "";
   settings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
   settings.StreamingCaptureMode = StreamingCaptureMode.Video;
   mediaCapture.VideoDeviceController.PrimaryUse = Windows.Media.Devices.CaptureUse.Video;

   // Initialize mediacapture now that settings are configured
   await mediaCapture.InitializeAsync(settings);

   if (mediaCapture.VideoDeviceController.TorchControl.Supported)
   {
      // Get resolutions and set to lowest available for temporary video file.
      var resolutions = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoRecord).Select(x => x as VideoEncodingProperties);
      var lowestResolution = resolutions.OrderBy(x => x.Height * x.Width).ThenBy(x => (x.FrameRate.Numerator / (double)x.FrameRate.Denominator)).FirstOrDefault();
      await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, lowestResolution);

     // Get resolutions and set to lowest available for preview.
     var previewResolutions =   mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties (MediaStreamType.VideoPreview).Select(x => x as VideoEncodingProperties);
     var lowestPreviewResolution = previewResolutions.OrderByDescending(x => x.Height * x.Width).ThenBy(x => (x.FrameRate.Numerator / (double)x.FrameRate.Denominator)).LastOrDefault();
     await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, lowestPreviewResolution);

     // Best practice, you should handle Media Capture Error events
     mediaCapture.Failed += MediaCapture_Failed;
     mediaCapture.RecordLimitationExceeded += MediaCapture_RecordLimitationExceeded;
    }
    else
    {
        // Torch not supported, exit method
        return;
    }

   // Start Preview
   var captureElement = new CaptureElement();
   captureElement.Source = mediaCapture;
   await mediaCapture.StartPreviewAsync();

   // Prep for video recording
   // Get Application temporary folder to store temporary video file folder
   StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;

   // Create a temp Flash folder 
   var folder = await tempFolder.CreateFolderAsync("TempFlashlightFolder", CreationCollisionOption.OpenIfExists);

   // Create video encoding profile as MP4 
   var videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);

   // Create new unique file in the Flash folder and record video
   var videoStorageFile = await folder.CreateFileAsync("TempFlashlightFile", CreationCollisionOption.GenerateUniqueName);

   // Start recording
   await mediaCapture.StartRecordToStorageFileAsync(videoEncodingProperties, videoStorageFile);

   // Now Toggle TorchControl property
   mediaCapture.VideoDeviceController.TorchControl.Enabled = true;
}

唷!这是为了切换闪光灯的很多代码吗?好消息是Windows 10中使用新的Windows.Devices.Lights.Lamp API修复了这个问题。您只需几行代码即可完成相同的工作: Windows 10 Sample for Windows.Devices.Lights.Lamp

供参考,请检查以下帖子: MSDN using Win8.1 VideoDeviceController.TorchControl