在Windows Phone 8.1中切换手电筒

Bal*_*i M 4 c# windows-phone-8.1

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

Rom*_*asz 6

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

// edit - I forgot to show GetCameraID:
private static async Task<DeviceInformation> 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;
Run Code Online (Sandbox Code Playgroud)

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


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

  • 尽管支持TorchControl,但对Lumia 930(8.1)不起作用. (2认同)