Windows phone 8.1 如何在windows phone 8.1上打开手电筒

Windows phone 8.1 如何在windows phone 8.1上打开手电筒,windows-phone-8.1,flashlight,Windows Phone 8.1,Flashlight,我创建的项目基于: 我的问题是如何在WP8.1上启用闪光灯(手电筒) 我应该使用MediaCapture()吗 当我用它的时候,它就掉下来了 var videoDev = mediaDev.VideoDeviceController; 通过未处理的异常 如何将手电筒添加到此示例项目中 您尚未初始化MediaCaptureSettings,因此当您尝试初始化videoController时,会发生异常。您需要初始化设置,让MediaCapture知道您要使用的设备,并设置VideoDevic

我创建的项目基于:

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

当我用它的时候,它就掉下来了

var videoDev = mediaDev.VideoDeviceController;
通过未处理的异常

  • 如何将手电筒添加到此示例项目中
您尚未初始化MediaCaptureSettings,因此当您尝试初始化videoController时,会发生异常。您需要初始化设置,让MediaCapture知道您要使用的设备,并设置VideoDeviceController。此外,对于Windows Phone 8.1摄像头驱动程序,有些要求您启动预览,有些要求您启动视频录制以打开闪光灯。这是因为闪光灯与相机设备紧密耦合

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

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;
}
呸!那有很多代码只是为了切换flash,对吧?好消息是,这在Windows 10中已通过新的Windows.Devices.Lights.Lamp API修复。您只需几行代码即可完成相同的工作:

请检查此线程以供参考:

例外情况是什么?你在什么设备上运行这个?您应该看看下面的CameraStarterKit示例。试着运行它,看看访问VideoDeviceController是否仍然会出现异常。我想指出,当USB电缆连接到PC时,即使在预览和录制时,火炬LED也会熄灭。我只是花了几个小时想弄清楚发生了什么。。。我的手机是Lumia 640 XL。
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;
}