如何在windows phone 8的应用程序中打开手电筒

本文关键字:应用程序 手电筒 windows phone | 更新日期: 2023-09-27 18:02:55

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

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上测试

请帮助我,非常感谢:)

如何在windows phone 8的应用程序中打开手电筒

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;
    }