locked
MediaCapture.initializeAsync fails if permission are denied

    Question

  • I try to build a quite simple camera app using the MediaCapture class. I found the following simple code in another answer and was also looking in the various samples like the CameraOptionsUI Sample.

    var mc = new Windows.Media.Capture.MediaCapture(); mc.initializeAsync().then( function () //success { var dialog = new Windows.Media.Capture.CameraCaptureUI(); dialog.captureFileAsync(Windows.Media.Capture.CameraCaptureUIMode.photo).then( function (file) { //successful }, function (err) { //failure, the access denied exception does not get passed here } ); }, function (err) //failure { console.log(err) //access denied here means that user has disallowed webcam access to this application
    // PROBLEM: THIS IS NOT USED BUT INSTEAD ERROR AS BELOW } );

    At first this works as expected, the system asks if the app may use the camera, I select yes and I can preview a video etc (so all capabilities etc should be set right). But when I remove the permission to use the camera from the apps settings charm and restart the app the errorHandler of initializeAsync is not used but instead I end up with this error in VisualStudio 2012:

    0x80070005 - JavaScript runtime error: Access is denied.
    WinRT information: Access is denied.

    So basically I see no way to catch the access is denied error. Am I doing something wrong or is this a bug? Newest VisualStudio 2012 11.0.51106.01 Update 1 on Win8 64 with a basic Microsoft webcam, or the same problem on a Surface RT. Actually I tried with the actual Sample and same behaviour there...

    Thanks for any help.

    Chris
    Sunday, March 10, 2013 4:31 PM

Answers

  • I played with your code as well as scenario 1 of the CameraCaptureUI sample. There are two things here.

    First, if you're using the CameraCaptureUI object, it's not necessary to call MediaCapture.initializeAsync yourself. The CameraCaptureUI sample shows that you can just call captureFileAsync directly. The MediaCapture.initializeAsync is for when you're creating your own UI, as the MediaCapture sample demonstrates separately.

    So in your code, you can remove the extra new/initializeAsync to simplify things.

    Second, the exception behavior you're seeing is coming from Visual Studio. If you hit Continue when the exception occurs in your code above, you'll end up in your outer error handler (for initializeAsync) with "Access is denied" in err.

    This is happening because Access denied exceptions are caught by Visual Studio by default when thrown, not only when unhandled (as nearly everything else is). Go to Debug/Exceptions... in VS, expand the JavaScript node, and you'll see the extra check on Thrown:

    Uncheck this and you'll head straight to your error handler.

    Of course, if you don't use initializeAsync as I mentioned above, you won't see this exception at all. Instead, the default capture UI will appear with a message about permissions, and when the user hits the back arrow you'll enter your success handler with a null in the file argument. This indicates that the UI was successfully displayed, but the user did no capture.

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press


    • Marked as answer by Christof7 Wednesday, March 13, 2013 1:43 PM
    Monday, March 11, 2013 5:54 PM

All replies

  • I played with your code as well as scenario 1 of the CameraCaptureUI sample. There are two things here.

    First, if you're using the CameraCaptureUI object, it's not necessary to call MediaCapture.initializeAsync yourself. The CameraCaptureUI sample shows that you can just call captureFileAsync directly. The MediaCapture.initializeAsync is for when you're creating your own UI, as the MediaCapture sample demonstrates separately.

    So in your code, you can remove the extra new/initializeAsync to simplify things.

    Second, the exception behavior you're seeing is coming from Visual Studio. If you hit Continue when the exception occurs in your code above, you'll end up in your outer error handler (for initializeAsync) with "Access is denied" in err.

    This is happening because Access denied exceptions are caught by Visual Studio by default when thrown, not only when unhandled (as nearly everything else is). Go to Debug/Exceptions... in VS, expand the JavaScript node, and you'll see the extra check on Thrown:

    Uncheck this and you'll head straight to your error handler.

    Of course, if you don't use initializeAsync as I mentioned above, you won't see this exception at all. Instead, the default capture UI will appear with a message about permissions, and when the user hits the back arrow you'll enter your success handler with a null in the file argument. This indicates that the UI was successfully displayed, but the user did no capture.

    Kraig

    Author, Programming Windows 8 Apps with HTML, CSS, and JavaScript, a free ebook from Microsoft Press


    • Marked as answer by Christof7 Wednesday, March 13, 2013 1:43 PM
    Monday, March 11, 2013 5:54 PM
  • aah, VS is still a bit new for me, guessed there might be something like this but never actually looked for it nor would have found it probably, thanks!

    I am using my own UI as I "stream" the video to a canvas element to manipulate it there. This is why I guess I need the MediaCapture.initializeAsync stuff

    BTW, I think I actually started using an example from your book originally :)

    Great book, currently only have the free Kindle version as it is still not out in Germany, Amazon still says 24.12.2012(!) which makes it a tiny bit delayed ;) :( I'd rather have it in paper...

    Monday, March 11, 2013 7:57 PM
  • Does work as expected on my Surface now. Thanks!

    I have a different problem on my desktop though. :(

    Neither my app nor the MediaCapture sample you mention above do work. Both fail with a dreaded "The text associated with this error could not be found" error with is not helpful at all. Webcam is a Microsoft LifeCam VX-2000 which strangely enough works fine with the in Win8 included Camera app but not with any JS app I try it with. Permission to webcam is also given too.

    Even stranger I think it worked some weeks ago so may be a driver problem but as the stock camera app works makes this mysterious.

    All stuff I search for on the net "blame" app code but as even the sample fails this got to be something else. Only other reason I found (on a comment only somewhere) saying something about a possible ATI driver update being to blame (I have an ATI graphics card).

    I tried to uninstall the Webcam driver and reconnected it. Same driver (from 2006) is used and still the webcam does not work.

    Any ideas what this problem might be? Also what this rather strange error message might be?

    Thanks for any hint!

    Monday, March 11, 2013 9:26 PM
  • Somehow the camera works now. Maybe the Win updates yesterday or a restart.

    Anyway, thanks!

    Wednesday, March 13, 2013 1:00 PM
  • The issue you had sounds like a driver problem, so a reboot would take care of that. Not too sure where the desktop is concerned.

    As for printed copies of my book, Microsoft Press decided last year to only do it as an ebook. So the listing on Amazon is likely wrong. Originally there was a plan for print on demand, but with all the hyperlinks and use of color in the book, that idea was scrapped. So don't hold your breath...

    Glad you got things working. VS can be a bit of a mystery if you're new to it. That's why I have the 10-minute VS intro video in the book's companion content, where I did point out the Exceptions dialog.

    .Kraig

    Wednesday, March 13, 2013 4:42 PM
  • Really sad for the book, I like reading the printed stuff even if it is more expensive :(

    But thanks for the info, do not have to wait anymore and have to read the Kindle version instead.

    Wednesday, March 13, 2013 5:17 PM