如何在Windows Phone 8上的应用程序中打开手电筒

时间:2014-05-13 02:00:19

标签: c# mobile windows-phone flashlight

当我按下开启按钮时,我想制作一个打开闪光灯的按钮,当我按下关闭按钮时关闭闪光灯。这是我的代码:

protected AudioVideoCaptureDevice Device { get; set; }

    private async void Button_Click_TurnOn(object sender, RoutedEventArgs e)
    {
        var sensorLocation = CameraSensorLocation.Back;

        try
        {
            // get the AudioViceoCaptureDevice
            var avDevice = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
                AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());

            // turn flashlight on
            var supportedCameraModes = AudioVideoCaptureDevice
                .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
            if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
            {
                avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);

                // set flash power to maxinum
                avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower,
                    AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
            }
            else
            {
                //ShowWhiteScreenInsteadOfCameraTorch();
            }

        }
        catch (Exception ex)
        {
            // Flashlight isn't supported on this device, instead show a White Screen as the flash light
            // ShowWhiteScreenInsteadOfCameraTorch();
        }
    }

    private void Button_Click_TurnOff(object sender, RoutedEventArgs e)
    {
        var sensorLocation = CameraSensorLocation.Back;

        try
        {
            // turn flashlight on
            var supportedCameraModes = AudioVideoCaptureDevice
                .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
            if (this.Device != null && supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.Off))
            {
                this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
            }
            else
            {
                //turnWhiteScreen(false);
            }
        }
        catch (Exception ex)
        {
            // Flashlight isn't supported on this device, instead show a White Screen as the flash light
            //turnWhiteScreen(false);
        }
    }

我从stackoverflow的另一个问题复制了它,但我不知道为什么这段代码对我不起作用。在Lumia 820上测试过。

请帮助我,非常感谢你:)

1 个答案:

答案 0 :(得分:0)

async private void FlashlightOn_Click(object sender, RoutedEventArgs e)
    {           
            // turn flashlight on
            CameraSensorLocation location = CameraSensorLocation.Back;
            if (this.audioCaptureDevice == null)
            {
                audioCaptureDevice = await AudioVideoCaptureDevice.OpenAsync(location,
                AudioVideoCaptureDevice.GetAvailableCaptureResolutions(location).First());
            }
            FlashOn(location, VideoTorchMode.On);

           }

        private void FlashlightOff_Click(object sender, RoutedEventAgrs e)
        {
            // turn flashlight off
            var sensorLocation = CameraSensorLocation.Back;
            FlashOn(sensorLocation, VideoTorchMode.Off);
        }


    public bool FlashOn(CameraSensorLocation location, VideoTorchMode mode)
    {
        // turn flashlight on/off
        var supportedCameraModes = AudioVideoCaptureDevice
            .GetSupportedPropertyValues(location, KnownCameraAudioVideoProperties.VideoTorchMode);
        if ((audioCaptureDevice != null) && (supportedCameraModes.ToList().Contains((UInt32)mode)))
        {
            audioCaptureDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, mode);
            return true;
        }
        return false;
    }