UWP提供的流编号无效PreviewState

时间:2017-04-13 22:32:07

标签: c# uwp

我正在尝试设置一个预览流并使用按钮记录循环以保存最后10分钟,30秒等。这一直很好,直到我开始添加代码来处理旋转。

这是抛出的线。

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

1 个答案:

答案 0 :(得分:2)

我可以在我这边重现你的问题,它会在代码行await _mediaCapture.SetEncodingPropertiesAsync(...);

引发错误异常
  

提供的流编号无效。   PreviewState

根据SetEncodingPropertiesAsync方法

  

请注意,此轮播由流的使用者执行,例如CaptureElement或视频播放器应用,而流中的实际像素仍保留其原始方向。

此方法由流的使用者执行。在设置预览旋转之前,您似乎需要首先调用StartPreviewAsync(),以便拥有预览流。更多细节请参考&#34;将方向数据添加到相机预览流&#34; 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