2017-04-13 52 views
2

プレビューストリームを設定しようとしていて、最後の10分、30秒などを保存するためのボタン付きのループを記録しようとしています。これは回転を処理するコードを追加するまで。UWP提供されたストリーム番号が無効だったPreviewState

これはスローする行です。ここ

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

が全体の方法でGoogle検索経由で見つかったソリューションの

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ハウツーの変更です。

EDIT:違反行を次のように変更すると正常に動作します。

_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

ありがとうございました。私は何か不具合がありました。私はできることを図書館に移そうとしていて、ここで終わった。小さなメソッドへの単純なリファクタリングではこれを解決し、正しいopsの順序を取得する必要があります。あなたの助けをもう一度ありがとう。 –

関連する問題