Windows Phone中的手电筒应用程序每次都会崩溃

3
我正在尝试通过Windows Phone应用程序中的TorchControl Class使用手电筒应用程序: 这是我的代码
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();          
   }

但问题在于,每次我第二次点击按钮时应用程序都会崩溃。我已经被告知使用mediaDev.Dispose()方法,但它也无效。 以下是异常:
“mscorlib.ni.dll中发生了类型为'System.Exception'的一次首次机会异常 WinRT信息:找不到与此错误码关联的文本。”
这是在“initializeasync”中突出显示的时候显示的。

1
什么是异常? - Sievajet
在“initializeasync”被标记时,发生了类型为“System.Exception”的第一次机会异常。WinRT信息:找不到与此错误代码相关联的文本。 - Prajjwal
请考虑编辑帖子以介绍这些细节。 - theMayer
你确定第二个按钮点击是在“dispose”被调用之后完成的吗? - fex
2个回答

2

如果重新初始化MediaCapture,将会抛出异常。为了解决这个问题,请确保在返回相机页面或单击相机按钮时不要重复初始化MediaCapture

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

同时,在您导航到其他页面之前,或在相机重新开始预览之前,请确保相机停止预览。无需处理MediaCapture对象。

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

GetCameraID 方法来源于 Romasz 的博客。 http://www.romasz.net/how-to-take-a-photo-in-windows-runtime/


1
这个问题可能与多线程有关:使用默认设置(即不更改SynchronizationContext)调用await会在另一个线程上继续执行方法,这并不总是由图形和媒体库支持(我亲身经历过SFML、WPF和AutoCAD等程序频繁崩溃)。虽然存在InitializeAsync方法,但请确保处理不需要在主线程或其他线程上进行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接