[英]What is the proper way to close a camera in Windows 8 Javascript?
我在javascript中使用MediaCapture捕捉我的相机。
我有一个带有initCamera函数的Camera类。 问题是,如果我尝试在短时间内重新启动我的相机,我将收到此错误: Hardware MFT failed to start streaming due to lack of hardware resources.
现在我知道这意味着我的相机仍在使用中。 我想知道的是:
这是一段代码:
function Camera() {
var that = this;
this.mediaCaptureElement = null;
this.initCamera = function() {
if (!that.mediaCaptureElement) {
that.mediaCaptureElement = new Windows.Media.Capture.MediaCapture();
that.mediaCaptureElement.addEventListener("failed", function (e) {
console.warn("The camera has stopped working");
}
that.mediaCaptureElement.initializeAsync().then(function() {
that.mediaCaptureElement.videoDeviceController.primaryUse = Windows.Media.Devices.CaptureUse.photo;
that.getCameraResolution();
that.orientationChanged();
that.startCamera();
});
}
};
我当前重新打开相机的方法是使用Camera类的新实例覆盖相机实例。
提前致谢。
我在C#
使用MediaCapture时遇到了同样的问题。
我必须在StopPreviewAsync
之后调用Dispose()
才能纠正它:
await cameraControler.MediaCaptureInstance.StopPreviewAsync(); cameraControler.MediaCaptureInstance.Dispose();
您是否看过相机入门套件UWP样品 ? 它也有JS风味!
如果您希望在完成相机使用后能够可靠地访问相机,则需要确保正确清理所有资源。 从您共享的代码中,您似乎正在让系统处理此问题,这意味着您的应用可能会在系统完成所有资源之前回来。
你应该照顾:
看看我上面链接的示例中的cleanupCameraAsync()方法 ,以获取有关如何实现它的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.