【问题标题】:Windows Phone 8 can´t call AudioVideoCaptureDevice second timeWindows Phone 8 无法第二次调用 AudioVideoCaptureDevice
【发布时间】:2014-01-05 01:46:57
【问题描述】:

在我的应用程序中,您可以打开一个站点,您可以在其中打开和关闭手电筒。 第一次它可以工作,但是如果我第二次尝试打开手电筒,应用程序就会崩溃。


我认为这是 AudioVideoCaptureDevice.OpenAsync 的问题。如果我再次调用它,应用程序会因 System.Reflection.TargetInvocationException WinRT-Informationen:无法获取相机而崩溃。你只能在前台使用这个类。

有人知道这个问题吗?

protected AudioVideoCaptureDevice Device { get; set; }

public Page10()
{
    InitializeComponent();
}

async void tglSwitch_Checked(object sender, RoutedEventArgs e)
{
    var sensorLocation = CameraSensorLocation.Back;
    if (this.Device == null)
    {
        // get the AudioVideoCaptureDevice
        this.Device = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
        AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
    }
    var supportedCameraModes = AudioVideoCaptureDevice
        .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
    if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
    {
        this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);

        // set flash power to maxinum
        this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower,
            AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);

        this.tglSwitch.Content = "Light on";
        this.tglSwitch.SwitchForeground = new SolidColorBrush(Colors.Green);
    }
}

void tglSwitch_Unchecked(object sender, RoutedEventArgs e)
{
    var sensorLocation = CameraSensorLocation.Back;
    sensorLocation = CameraSensorLocation.Back;
    var supportedCameraModes = AudioVideoCaptureDevice
        .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
    if (this.Device != null && supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.Off))
    {
        this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
        this.tglSwitch.Content = "Light off";
    }
}

【问题讨论】:

  • 您能否尝试避免第二次调用 AudioVideoCaptureDevice.OpenAsync 维护对 this.Device 的引用以重新利用它?

标签: c# windows-phone-8 flashlight


【解决方案1】:

我建议在页面生命周期中使用 OpenAsync 一次初始化相机,例如在 OnNavigatedTo 事件中。并且只有 makeSetProperty() 方法调用复选框事件中的代码来控制灯光。正确处理相机然后离开页面也非常重要,例如在OnNavigatedFrom 事件中,通过调用device.Dispose()。此选项还可以让您的手电筒工作得更快。

请记住,Windows Phone 8.1 现在有用于 Torch 的专用 API,效果很好,代码也更漂亮。您也可以在 Silverlight 项目中使用,但您必须迁移您的项目。这里有更多关于 http://developer.nokia.com/community/wiki/Using_the_camera_light_in_Windows_Phone_7,_8_and_8.1https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.devices.torchcontrol 的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    相关资源
    最近更新 更多