使用Windows Phone的8.1 MediaCapture类。
已声明“音频”和“网络摄像头”的功能,90%的异常原因可能是这个。
问题是,在WP模拟器中它工作得很好,但在实际设备上会出错。
确切的异常在这里:
我已经添加了很多检查,以确保我们不会重新初始化已经初始化的相机或在初始化之前尝试读取等等(因为我认为问题是由此引起的)。因此,这种情况发生的可能性非常小。
private async Task InitializeCameraAsync()
{
if (_isInitialized)
{
Debug.WriteLine("Skipping unnecessary initialization");
return;
}
Debug.WriteLine("Initializing camera media capture...");
_deviceCapture = new MediaCapture();
await _deviceCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
VideoDeviceId = _cameraInfoCollection[_currentVideoDevice].Id,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = _microphoneInfoCollection[_currentAudioDevice].Id
StreamingCaptureMode = StreamingCaptureMode.Video
});
Debug.WriteLine("Initialized camera media capture!");
// For code completion only, unlikely to be relevant
// Set up low-lag photo capture
if (IsNotUsingInstantCapture)
{
Debug.WriteLine("Preparing low-lag photo capture");
var imageEncoding = ImageEncodingProperties.CreateJpeg();
imageEncoding.Width = PhotoCaptureWidth;
imageEncoding.Height = PhotoCaptureHeight;
_lowLagPhotoCapture = await _deviceCapture.PrepareLowLagPhotoCaptureAsync(imageEncoding);
}
_isInitialized = true;
Debug.WriteLine("Initialized camera!");
}
_mediacapture 然后被绑定到一个 XAML CaptureElement 的 .source 上,以显示预览。