【问题标题】:how to turn on flashlight on windows phone 8.1windows phone 8.1如何打开手电筒
【发布时间】:2015-07-22 11:43:32
【问题描述】:

我创建的项目基于: 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;

由于未处理的异常

  • 如何在这个示例项目中添加手电筒?

【问题讨论】:

  • 有什么异常?你在什么设备上运行这个?您应该查看 github.com/Microsoft/Windows-universal-samples 下的 CameraStarterKit 示例。尝试运行它,看看访问 VideoDeviceController 是否仍然给你一个异常。

标签: windows-phone-8.1 flashlight


【解决方案1】:

您尚未初始化 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

【讨论】:

  • 我想指出的是,当 USB 数据线连接到 PC 时,即使在预览和录制时,手电筒 LED 也会保持不亮。我花了几个小时试图弄清楚发生了什么……我的手机是 Lumia 640 XL。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多