2015-01-10 97 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/

相關問題