2015-02-23 72 views
3

我在JavaScript中使用MediaCapture捕捉我的相机。
我有一个带有initCamera函数的Camera类。问题是,如果我尝试在短时间内重新启动相机,我会得到这个错误:Hardware MFT failed to start streaming due to lack of hardware resources.在Windows 8 Javascript中关闭相机的正确方法是什么?

现在我明白了,这意味着我的相机仍在使用中。我想知道的事情是:

  • 如何正确地关闭我的相机
  • 如何检查我的相机正在使用或无法使用

下面是一段代码:

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

当前我重新打开相机的方式是用相机类的新实例覆盖相机实例。

在此先感谢。

+1

你有试过关掉它吗? – Kevin 2015-02-23 09:59:43

回答

1

我在使用C#中的MediaCapture时遇到了同样的问题。

我不得不为了StopPreviewAsync后打电话到Dispose()加以纠正:

await cameraControler.MediaCaptureInstance.StopPreviewAsync(); cameraControler.MediaCaptureInstance.Dispose();

1

你见过Camera Starter Kit UWP sample?它也有JS的味道!

如果您希望在使用完相机后能够可靠地使用相机,则需要确保您正确清理所有资源。从您分享的代码中,似乎让系统处理这个问题,这意味着您的应用可能会在系统完成关闭所有资源之前回来。

你应该照顾:

  • 停止录音,可能是正在进行
  • 停止预览
  • 关闭MediaCapture

看一看的cleanupCameraAsync() method从我上面链接了一个关于如何实现这个的例子。

相关问题