Question

In my App you can open a Site where you can switch on and off the Flashlight. The first time it works, but if I try to switch the flashlight on a second time the App crashes.


I think this is a Problem with AudioVideoCaptureDevice.OpenAsync. If I call it a second time the App crashes with a System.Reflection.TargetInvocationException WinRT-Informationen: Unable to acquire the camera. You can only use this class while in the foreground.

Someone know this Problem?

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";
    }
}
Was it helpful?

Solution

I would recommend to initialize the camera with OpenAsync ONE TIME in page lifecycle, for example in OnNavigatedTo event. And only makeSetProperty() methods calls code in your checkbox events to control light. It is also very important to dispose camera correctly then leaving the page, for example in OnNavigatedFrom event, by calling device.Dispose(). This option also make your flashlight to work faster.

Keep in mind that Windows Phone 8.1 now has dedicated API for torch, which works great and the code is more beautiful. You can use in Silverlight project as well, but you have to migrate your project. Here is more about this http://developer.nokia.com/community/wiki/Using_the_camera_light_in_Windows_Phone_7,_8_and_8.1 and https://msdn.microsoft.com/en-us/library/windows/apps/windows.media.devices.torchcontrol.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top