2017-04-13 140 views
2

我想設置一個預覽流和記錄循環的按鈕來保存最後10分鐘,30秒等等。這工作得很好,直到我開始添加代碼來處理旋轉。UWP提供的流號碼無效PreviewState

這是拋出的線。

await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, 
videoEncodingProperties, mediaPropertySet); 

這裏是整個方法

public async Task<MediaCapture> PrepareRecordingAsync() { 
      try { 
       _mediaCapture = new MediaCapture(); 
       var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); 
       var desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Panel.Back); 
       _cameraDevice = desiredDevice ?? allVideoDevices.FirstOrDefault(); 
       _rotationHelper = new CameraRotationHelper(_cameraDevice.EnclosureLocation); 

       _mediaCapture.Failed += MediaCapture_Failed; 

       var settings = new MediaCaptureInitializationSettings { VideoDeviceId = _cameraDevice.Id }; 
       await _mediaCapture.InitializeAsync(settings); 

       var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); 

       var rotationAngle = CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(_rotationHelper.GetCameraCaptureOrientation()); 
       Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1"); 
       encodingProfile.Video.Properties.Add(RotationKey, PropertyValue.CreateInt32(rotationAngle)); 
       var videoEncodingProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); 
       MediaPropertySet mediaPropertySet = new MediaPropertySet(); 
       await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, videoEncodingProperties, mediaPropertySet); 

       _ras = new InMemoryRandomAccessStream(); 
       _recording = await _mediaCapture.PrepareLowLagRecordToStreamAsync(encodingProfile, _ras); 

       DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait; 
       ConcurrentRecordAndPhotoSupported = _mediaCapture.MediaCaptureSettings.ConcurrentRecordAndPhotoSupported; 
      } catch (UnauthorizedAccessException) { 
       // This will be thrown if the user denied access to the camera in privacy settings 
       System.Diagnostics.Debug.WriteLine("The app was denied access to the camera"); 
      } catch (Exception ex) { 
       System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed. {0}", ex.Message); 
      } 
      return _mediaCapture; 
     } 

沒有通過谷歌搜索找到的解決方案是任何幫助。

這基本上是對MSDN操作指南的修改。

編輯:如果我改變了違規行以下然後它工作正常。

_mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees); 

回答

2

我可以重現我的身邊您的問題,它會拋出錯誤異常的代碼行await _mediaCapture.SetEncodingPropertiesAsync(...);

提供的流號碼無效。 PreviewState

根據

注意,這種旋轉是由流,如CaptureElement或視頻播放應用程序的消費者進行的,而物流中的實際像素仍然是SetEncodingPropertiesAsync方法保留原來的方向。

此方法由流的消費者執行。看起來您需要首先調用StartPreviewAsync(),然後才能設置預覽旋轉,以便預覽流。更多詳細信息,請參考Handle device orientation with MediaCapture的「將定向數據添加到相機預覽流」部分。

開始預覽後,調用助手方法SetPreviewRotationAsync設置預覽旋轉。

因此,更新您的代碼片段如下,它將工作。

_mediaCapture = new MediaCapture(); 
var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); 
var desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back); 
_cameraDevice = desiredDevice ?? allVideoDevices.FirstOrDefault(); 
_rotationHelper = new CameraRotationHelper(_cameraDevice.EnclosureLocation); 
_mediaCapture.Failed += MediaCapture_Failed; 
var settings = new MediaCaptureInitializationSettings { VideoDeviceId = _cameraDevice.Id }; 
await _mediaCapture.InitializeAsync(settings); 

//Add the preview code snippet 
PreviewControl.Source = _mediaCapture; 
await _mediaCapture.StartPreviewAsync(); 

var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); 
var rotationAngle = CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(_rotationHelper.GetCameraCaptureOrientation()); 
Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1"); 
encodingProfile.Video.Properties.Add(RotationKey, PropertyValue.CreateInt32(rotationAngle)); 
var videoEncodingProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); 
MediaPropertySet mediaPropertySet = new MediaPropertySet(); 
await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, videoEncodingProperties, mediaPropertySet); 

更多詳情請參考official sample

+0

謝謝,我想我得到了一些亂七八糟的東西。我試圖把我能夠到達的圖書館搬到這裏。對較小的方法進行一些簡單的重構應該可以解決這個問題,並得到正確的操作順序。再次感謝你的幫助。 –