2015-02-23 68 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從我上面鏈接了一個關於如何實現這個的例子。

相關問題