Flashlight app flash every time on Windows Phone

I am trying to use a flashlight app through the TorchControl class in a Windows Phone app: Here is my code

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 {0} doesn't exist", desiredCamera));
    }


    async private void Button_Click(object sender, RoutedEventArgs e)
   {
       var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
       var mediaDev = new MediaCapture();
       await mediaDev.InitializeAsync(new MediaCaptureInitializationSettings
       {
           StreamingCaptureMode = StreamingCaptureMode.Video,
           PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
           AudioDeviceId = String.Empty,
           VideoDeviceId = cameraID.Id
       });
       var videoDev = mediaDev.VideoDeviceController;
       var tc = videoDev.TorchControl;
       if (tc.Supported)         
           tc.Enabled = true;
       mediaDev.Dispose();          
   }

      

But the problem is the app crashes every time I press the button a second time. I was told to use mediaDev.Dispose () method but it doesn't work either. Here's the exception:

The first exception of type "System.Exception" occurred in mscorlib.ni.dll WinRT Information: The text associated with this error code was not found.

  • Shown when the text in "initializeasync" is highlighted.
+3


source to share


2 answers


MediaCapture will throw an exception when it is reinitialized. To fix this problem, just make sure that you don't initialize MediaCapture twice when you go to the camera page or when you click the camera button.

    MediaCapture mediacapture = new MediaCapture();
    bool initialized;
    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (initialized == false)
        {
            var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
            await mediacapture.InitializeAsync(new MediaCaptureInitializationSettings
            {
                StreamingCaptureMode = StreamingCaptureMode.Video,
                PhotoCaptureSource = PhotoCaptureSource.Photo,
                AudioDeviceId = string.Empty,
                VideoDeviceId = cameraID.Id
            });
        }
        //Selecting Maximum resolution for Video Preview
        var maxPreviewResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).Aggregate((i1, i2) => (i1 as VideoEncodingProperties).Height > (i2 as VideoEncodingProperties).Height ? i1 : i2);
        //Selecting 4rd resolution setting
        var selectedPhotoResolution = mediacapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).ElementAt(3);
        await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, selectedPhotoResolution);
        await mediacapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, maxPreviewResolution);
        // in my .xaml <CaptureElement Name="viewfinder" />
        viewfinder.Source = mediacapture;
        mediacapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
        await mediacapture.StartPreviewAsync(); 
        initialized = true;
    }

      

Also, make sure the camera stops viewing before navigating to another page or before re-viewing the camera. No need to host MediaCapture.



    private async void GoBack_Click(object sender, RoutedEventArgs e)
    {     
        await mediacapture.StopPreviewAsync();
        this.Frame.Navigate(typeof(MainPage));
        //Not needed
        //mediacapture.Dispose();
    }

      

GetCameraID method for Romasz magazine. http://www.romasz.net/how-to-take-a-photo-in-windows-runtime/

+2


source


This issue can be related to multithreading: using defaults (i.e. not changing SynchronizationContext

) calls await

will not continue methods in another thread, which is not always supported by graphics and media libraries (I have first-hand experience with SFML, WPF and AutoCAD is very happy to name a few). While the presence of a method InitializeAsync

indicates otherwise, make sure the deletion should not happen on the main thread or such.



+1


source







All Articles