简体   繁体   中英

What is the proper way to close a camera in Windows 8 Javascript?

I'm using MediaCapture in javascript to capture my camera.
I have a Camera class with an initCamera function. The problem is, if I try to re-init my camera in a short time period I will get this error: Hardware MFT failed to start streaming due to lack of hardware resources.

Now I get that this means my camera is still in use. The thing I want to know is:

  • How do I properly close my camera
  • How do I check if my camera is in use or unavailable

Here is a piece of code:

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

The way I re-open my camera currently is by overwriting the camera instance with a new instance of the Camera class.

Thanks in advance.

I had the same problem using MediaCapture in C# .

I had to call Dispose() after StopPreviewAsync in order to correct it :

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

Have you seen the Camera Starter Kit UWP sample ? It comes in a JS flavor too!

If you want to be able to reliably access the camera shortly after being done using it, you need to make sure you're cleaning up all resources properly. From the code that you've shared, it seems like you're letting the system take care of this, which means your app might be coming back before the system is done closing out all resources.

You should take care of:

  • Stop any recordings that may be in progress
  • Stop the preview
  • Close the MediaCapture

Have a look at the cleanupCameraAsync() method from the sample I linked above for an example on how to implement this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM