UWP提供的流号码无效PreviewState
问题描述:
我想设置一个预览流和记录循环的按钮来保存最后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);
答
我可以重现我的身边您的问题,它会抛出错误异常的代码行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。
谢谢,我想我得到了一些乱七八糟的东西。我试图把我能够到达的图书馆搬到这里。对较小的方法进行一些简单的重构应该可以解决这个问题,并得到正确的操作顺序。再次感谢你的帮助。 –