locked
Closing and re-initializing MediaCapture inside the application

    Question

  • I'm using the following code to initialize the camera and preview the feed:

        function startCamera() {
            livePreview = document.getElementById("live-preview"); // div
    
            mediaCapture = new Windows.Media.Capture.MediaCapture();
            mediaCapture.initializeAsync().then(function () {
                livePreview.src = URL.createObjectURL(mediaCapture);
                livePreview.play();
                
            });
        }

    Somewhere in the code I want to close the camera feed and open it again. I tried to use the close() method as below:

    mediaCapture.close();

    This will successfully close the MediaCapture (I can see the camera LED indicator will be turned off) but if I call the startCamera() again the feed will start but I will immediately get a win32 exception:

    Unhandled exception at 0x00007FFA71EEAB78 (KernelBase.dll) in WWAHost.exe:
    0xC0000002: The requested operation is not implemented.

    I want to know what is the correct way to close and re-open the camera feed inside a single application page. I know that if the feed is opened in another page, the windows will automatically deallocate the camera.

    Thanks :)


    Friday, January 24, 2014 2:11 AM

Answers

  • Hello,

    You don't want to create a new instance of the MediaCapture object if it already exists and is associated with a particular camera. This is due to the fact that "Close" simply stops the capture but may not release the reference to the device. In other words you should create a single MediaCapture object that is used for the lifetime of your app.

    I hope this helps,

    James


    Windows SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Saturday, January 25, 2014 2:00 AM
    Moderator
  • Well, that helped a lot. I realized that the camera starts when the "src" attribute is assigned with the MediaCapture's URL (not when "play" is called). All I had to do to stop the camera was to assign an empty string to "src".

    However you have to make sure that MediaCapture is initialized  whenever the application is launched and also whenever it is reactivated from suspension. For simplification, I am initializing it each time before using the camera. Here is the code:

    // start + play
    livePreview = document.getElementById("live-preview"); // video tag     
    mediaCapture = new Windows.Media.Capture.MediaCapture();
    mediaCapture.initializeAsync().then(function () {
         livePreview.src = URL.createObjectURL(mediaCapture);
         livePreview.play();
    });
                
    
    
    // pause + stop
    livePreview.pause();
    livePreview.src = "";
                

    Thanks James :)

    • Marked as answer by Farshid.T Thursday, January 30, 2014 4:37 AM
    Thursday, January 30, 2014 4:35 AM

All replies

  • Hello,

    You don't want to create a new instance of the MediaCapture object if it already exists and is associated with a particular camera. This is due to the fact that "Close" simply stops the capture but may not release the reference to the device. In other words you should create a single MediaCapture object that is used for the lifetime of your app.

    I hope this helps,

    James


    Windows SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Saturday, January 25, 2014 2:00 AM
    Moderator
  • Thanks James,

    In this case how can I open the MediaCapture object once it's been closed? I am creating the object and initializing it once but I need to close/open it several times in the lifetime on my app.

    (Based on the example here, it looks like StartPreviewAsync and StopPreviewAsync are the suitable functions but they are not available in Javascript)

    Sunday, January 26, 2014 3:32 AM
  • Hello,

    Thanks for the clarification I think I understand you question more clearly now. You can't use Start/StopPreviewAsync from JavaScript because these functions are closely tied to the video preview XAML control. Honestly I have do admit I'm really not sure. I can offer a guess though. Have you tried using "pause" and "play" on your hosing video tag? That is probably as close as we are going to get. If that doesn't work let me know and we will try to find another solution for you.

    Thanks,

    James 


    Windows SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Wednesday, January 29, 2014 11:44 PM
    Moderator
  • Well, that helped a lot. I realized that the camera starts when the "src" attribute is assigned with the MediaCapture's URL (not when "play" is called). All I had to do to stop the camera was to assign an empty string to "src".

    However you have to make sure that MediaCapture is initialized  whenever the application is launched and also whenever it is reactivated from suspension. For simplification, I am initializing it each time before using the camera. Here is the code:

    // start + play
    livePreview = document.getElementById("live-preview"); // video tag     
    mediaCapture = new Windows.Media.Capture.MediaCapture();
    mediaCapture.initializeAsync().then(function () {
         livePreview.src = URL.createObjectURL(mediaCapture);
         livePreview.play();
    });
                
    
    
    // pause + stop
    livePreview.pause();
    livePreview.src = "";
                

    Thanks James :)

    • Marked as answer by Farshid.T Thursday, January 30, 2014 4:37 AM
    Thursday, January 30, 2014 4:35 AM