locked
Please give an example of how to use MediaCapture class, the sdk example does not work (can only works in remote desktop)

    Question

  • Please give an example of how to use MediaCapture class, the sdk example does not work (can only works in remote desktop).
    Monday, July 09, 2012 2:41 AM

Answers

  • The MediaCapture object's initializeAsync method is asynchronous.  So, since it is not part of your promise chain, there is no guarantee it has completed when you attempt to call startRecordToStorageFileAsync.

    Try including initializeAsync in your promise chain, like this:

    mediaCapture.initializeAsync(mediaCaptureInitializationSettings).then(function () {
        return Windows.Storage.KnownFolders.videosLibrary.createFileAsync("cameraCapture.mp4", Windows.Storage.CreationCollisionOption.generateUniqueName);
    }).then(function (newFile) {
        var encodingProfile = Windows.Media.MediaProperties.MediaEncodingProfile.createMp4(Windows.Media.MediaProperties.VideoEncodingQuality.auto); 
        return mediaCapture.startRecordToStorageFileAsync(encodingProfile, newFile);
    }).done(function () {
        console.log('finished !');
    });
    

    Monday, July 09, 2012 9:09 PM

All replies

  • Hi, I am able to run "CameraCaptureUI Sample" and "CameraOptionsUI Sample", are they work for you?

    http://code.msdn.microsoft.com/windowsapps/CameraCaptureUI-Sample-845a53ac

    http://code.msdn.microsoft.com/windowsapps/CameraOptionsUI-Sample-44c06873

    Monday, July 09, 2012 5:29 AM
  • different one Betterlife, what I want is Media Capture, not the Camera Capture.
    Monday, July 09, 2012 6:30 AM
  • woah, no Microsoft employee to answer ?

    They used to be super fast when answering questions. Is it holiday in USA ?

    Monday, July 09, 2012 6:36 AM
  • How about this one "Media capture using webcam sample"?

    http://code.msdn.microsoft.com/windowsapps/Media-Capture-Sample-adf87622

    They are in midnight now.

    Monday, July 09, 2012 6:46 AM
  • yeah, that one, the problem is, it doesn't work. It only works for "remote desktop", and I don't know how to do that, i'm using laptop. And I tried to write my own code while looking at that code, it gives an error. So I was hoping someone here that already knows MediaCapture class would help me, I really need the help.
    Monday, July 09, 2012 6:50 AM
  • Hi I modified the sample, make it just use JS+HTML, it works in my pc. This sample is able to preview and take a photo.

    You could get it from: http://sdrv.ms/MUtgDJ

    I use the latest W8 build, maybe you need change some config.

    Monday, July 09, 2012 8:27 AM
  • In the sample project, change the processor type from ARM to x86 or x64.  Then you will be able to change to 'Local Machine' and run the sample.

    If the preview does not work in the sample, what is your camera make and model?

    -Jeff


    Jeff Sanders (MSFT)

    Monday, July 09, 2012 12:34 PM
    Moderator
  • it works now, I did not know I had to change ARM to x64. jpsanders, can you gives me a simple code example on how to use the MediaCapture simply to record then saves it into a file let's say in Music folder, I keep getting errors saying I had to "initialize MediaCapture object", I don't even know what i'm doing wrong.

    I tried to copy paste the code Example (rewriting it), but I must make a mistake somewhere, because I keep getting error when I started to record+save to file.

    Monday, July 09, 2012 1:16 PM
  • here is the code on default.js

    (function () {
        "use strict";
    
        var app = WinJS.Application;
        var activation = Windows.ApplicationModel.Activation;
        WinJS.strictProcessing();
    
        app.onactivated = function (args) {
    
            if (args.detail.kind === activation.ActivationKind.launch) {
    
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
    
                    var mediaCapture = new Windows.Media.Capture.MediaCapture();
                    //sets initialization
                    var mediaCaptureInitializationSettings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
                    mediaCaptureInitializationSettings.audioDeviceId = "";
                    mediaCaptureInitializationSettings.videoDeviceId = "";
                    mediaCaptureInitializationSettings.streamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.audioAndVideo;
                    mediaCaptureInitializationSettings.photoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.videoPreview;
                    mediaCaptureInitializationSettings.realTimeModeEnabled = true;
    
                    mediaCapture.initializeAsync(mediaCaptureInitializationSettings);
    
                    Windows.Storage.KnownFolders.videosLibrary.createFileAsync("cameraCapture.mp4", Windows.Storage.CreationCollisionOption.generateUniqueName).then(function (newFile) {
    
                        var encodingProfile = Windows.Media.MediaProperties.MediaEncodingProfile.createMp4(Windows.Media.MediaProperties.VideoEncodingQuality.auto);
                        mediaCapture.startRecordToStorageFileAsync(encodingProfile, newFile).done(function () {
                            console.log('finished !');
                        });
                    });
    
    
                   
    
                   
                } else {
    
                    // This application has been reactivated from suspension. Restore application state here.
                }
                args.setPromise(WinJS.UI.processAll());
            }
        };
    
        app.start();
    })();

    The error is :

    Exception was thrown but not handled in user code at line 28, column 21 in ms-appx://1635a84c-7b10-4678-8389-34aefae706f0/default.js
    
    0xc00d36b6 - JavaScript runtime error: The text associated with this error code could not be found.
    
    WinRT information: This object needs to be initialized before the requested operation can be carried out.

    Monday, July 09, 2012 1:23 PM
  • jpsanders, can you please gives me just a very very simple, very basic example of using MediaCapture class ? I can take it myself after I know how to use the class. The above code does not work, and I don't even know why.
    Monday, July 09, 2012 1:33 PM
  • Did the sample work for you?

    If so, this quickstart is the simplest and should work fine for you:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh452791.aspx

    If the sample did not work, your camera driver may not be installed or not supported in Windows 8.

    -Jeff


    Jeff Sanders (MSFT)

    Monday, July 09, 2012 6:58 PM
    Moderator
  • The MediaCapture object's initializeAsync method is asynchronous.  So, since it is not part of your promise chain, there is no guarantee it has completed when you attempt to call startRecordToStorageFileAsync.

    Try including initializeAsync in your promise chain, like this:

    mediaCapture.initializeAsync(mediaCaptureInitializationSettings).then(function () {
        return Windows.Storage.KnownFolders.videosLibrary.createFileAsync("cameraCapture.mp4", Windows.Storage.CreationCollisionOption.generateUniqueName);
    }).then(function (newFile) {
        var encodingProfile = Windows.Media.MediaProperties.MediaEncodingProfile.createMp4(Windows.Media.MediaProperties.VideoEncodingQuality.auto); 
        return mediaCapture.startRecordToStorageFileAsync(encodingProfile, newFile);
    }).done(function () {
        console.log('finished !');
    });
    

    Monday, July 09, 2012 9:09 PM
  • ah that is very helpful Caroline G, thanks. The documentation needs more examples, the current examples has waaay too much dependence on Pages namespace and other namespace instead of focusing on just on Media. 

    Friday, July 13, 2012 7:35 PM
  • Hi


    I have initialized the MediaCapture Device using the following code snippet

      MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();
                    settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
                    settings.AudioDeviceId = RecordingDeviceInformation.Id;

     CaptureManager = new MediaCapture();

     await CaptureManager.InitializeAsync(settings);

    RecordingDeviceInformation is from one of the below listed Devices

    Device \\?\SWD#MMDEVAPI#{0.0.1.00000000}.{0fbb95f4-46ed-4085-9d31-a8dc70b66d2e}#{2eef81be-33fa-4800-9670-1cd474972c3f} Handset (QCAUD Real Time (WaveRT) Driver for Qualcomm 8x60 SoC) False
    Device \\?\SWD#MMDEVAPI#{0.0.1.00000000}.{12728e58-985e-402e-918f-1e4012d9ca00}#{2eef81be-33fa-4800-9670-1cd474972c3f} Microphone (QCAUD Real Time (WaveRT) Driver for Qualcomm 8x60 SoC) False
    Device \\?\SWD#MMDEVAPI#{0.0.1.00000000}.{0a5af8e3-bb32-4dc3-9b81-b2afbd2e740a}#{2eef81be-33fa-4800-9670-1cd474972c3f} Enhanced Audio Recording Device (QCAUD Real Time (WaveRT) Driver for Qualcomm 8x60 SoC) False

    but when the below call is made  

    CaptureManager.StartRecordToStorageFileAsync(ProfileInfo,fileDetails)

    It is throwing the following exception

    A first chance exception of type 'System.Exception' occurred in the App
    WinRT information: This object needs to be initialized before the requested operation can be carried out.

    This object needs to be initialized before the requested operation can be carried out

    It use to be working when rolled out but I have upgraded my phone to 8.1.


    • Edited by VMDevs Tuesday, August 05, 2014 5:25 AM
    Tuesday, August 05, 2014 5:24 AM