我试图通过按钮设置预览流和录制循环,以保存最后的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;
}
通过Google搜索找到的解决方案均无济于事。
这基本上是对MSDN操作方法的修改。
编辑:如果我将违规行更改为以下内容,则可以正常工作。
_mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
最佳答案
我可以重现您的问题,它将在代码行await _mediaCapture.SetEncodingPropertiesAsync(...);
上引发错误异常
提供的流编号无效。
预览状态
根据SetEncodingPropertiesAsync
方法
请注意,此旋转是由流的使用者(例如CaptureElement或视频播放器应用程序)执行的,而流中的实际像素仍保持其原始方向。
此方法由流的使用者执行。似乎您需要首先调用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。
关于c# - UWP提供的流号无效PreviewState,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43402721/