2015-11-26 187 views
2

我的问题很简单。在Windows 10上打开手电筒

我想在Windows 10通用应用程序项目上打开闪存(并保持打开状态),但没有任何结果我尝试工作。

这是代码

MediaCapture MyMediaCapture = new MediaCapture(); 

var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); 
DeviceInformation cameraDevice = 
     allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && 
     x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back); 
cameraDevice = cameraDevice ?? allVideoDevices.FirstOrDefault(); 

if (cameraDevice == null) 
{ 
    Debug.WriteLine("No camera device found!"); 
} 
else 
{ 
    await MyMediaCapture.InitializeAsync(new MediaCaptureInitializationSettings 
    { 
     VideoDeviceId = cameraDevice.Id 
    }); 

    var MyVideoDeviceController = MyMediaCapture.VideoDeviceController; 
    var MyTorch = MyVideoDeviceController.TorchControl; 

    if (MyTorch.Supported) 
    { 
     var captureElement = new CaptureElement(); 
     captureElement.Source = MyMediaCapture; 
     await MyMediaCapture.StartPreviewAsync(); 

     FileStream tmp = new FileStream(System.IO.Path.GetTempFileName() + Guid.NewGuid().ToString() + ".mp4", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 10000, FileOptions.RandomAccess | FileOptions.DeleteOnClose); 

     var videoFile = await KnownFolders.VideosLibrary.CreateFileAsync(tmp.Name, CreationCollisionOption.GenerateUniqueName); 

     var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Wvga); 

     await MyMediaCapture.StartRecordToStorageFileAsync(encodingProfile, videoFile); 

     MyTorch.PowerPercent = 100; 
     MyTorch.Enabled = true; 
    } 
} 

编辑:添加代码

回答

0

你在正确的道路上。根据设备的不同(由于驱动程序的具体实现),您必须开始预览,或者甚至可以启动视频录制环节以打开灯光。

因此,为了保证与大多数设备的兼容性,我建议您实际上都采用这两种设备。

+0

我改变了我的代码,但我不太确定我的代码是否正确,因为我是初学者 – sami54

+0

看看这个示例:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/CameraManualControls。在Flash下,启用视频灯/手电筒。 – Mike

+0

谢谢,在此示例的帮助下,我打开手电筒 – sami54

2

它看起来像你正在尝试使用访问,我们再也不用在Windows 10 UWP使用手电筒的老方法发展。看看Windows.Devices.Lights in this sample on GitHub中的新灯泡功能。

这是使用独立于访问相机API的闪光灯的绝佳起点。

+0

我试过你在我的lumia 930上提出的建议,但它不起作用...没有找到灯 – sami54

+0

Lumia 930是Win 8.1设备,因此它不支持此答案中的新API。虽然Lumia 950和950XL(和未来的设备)应该。 – Mike

+0

即使我在Lumia 930(vers:10.0.10586.11)上安装了Windows 10,它也不支持新的API。 – sami54