2015-01-10 64 views
3

我试图运行在Windows Phone应用程序通过TorchControl类手电筒应用: 这里是我的代码手电筒应用程序崩溃每次

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()方法,但它也不起作用。 这里的例外:

类型的第一个机会异常“System.Exception的”发生在 mscorlib.ni.dll WinRT的信息:与此错误 代码关联的文本无法被发现。

  • 而在文本 “initializeasync” 突出显示

    请帮助这是显示。谢谢。使用默认值(即不改变SynchronizationContext)调用await将继续在另一个线程的方法,一些东西,并不总是由图形和媒体库支持(我亲眼:

+1

什么是例外? – Sievajet

+0

“mscorlib.ni.dll中发生第一次机会异常类型'System.Exception' WinRT信息:无法找到与此错误代码关联的文本。” - 当“initializeasync”中的文本突出显示时显示 – Prajjwal

+0

请考虑编辑帖子以介绍这些详细信息。 – theMayer

回答

1

这个问题可能与多线程使用SFML,WPF和AutoCAD的经验让人非常高兴,仅举几例)。虽然InitializeAsync方法的存在表示了其他情况,但确保处理不需要在主线程或其他方面发生。

2

MediaCapture将在重新初始化时引发异常。要解决此问题,只需确保不会在您导航回Camera页面或单击相机按钮时初始化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/

相关问题