locked
Cannot using MediaCapture to record a mp3 file from the audio device

    Question

  • Hello guys,

    I would to ask your professional experience to solve my issue. I'm trying to record a mp3 file using the audio device (microphone) and MediaCapture class.

    I have found a few link and resources how it works, something like http://msdn.microsoft.com/en-us/library/windows/apps/hh452798.aspx and http://code.msdn.microsoft.com/windowsapps/Media-Capture-Sample-adf87622/

    Unfortunately it doesn't work for me. As a result the media file (audioCapture.mp3 or audioCapture.m4a) has been created but it is always empty.

    I also was trying to use some different settings to MediaCapture, here is just one example:

    captureInitSettings = null;
    captureInitSettings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
    captureInitSettings.audioDeviceId = "";        
    captureInitSettings.videoDeviceId = "";
    captureInitSettings.streamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.audio;
    captureInitSettings.photoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.videoPreview;
    captureInitSettings.realTimeModeEnabled = true;
    if (deviceList.length > 0)
        captureInitSettings.audioDeviceId = deviceList[0].id;

    But after that the following code just creating an empty file:

    Windows.Storage.KnownFolders.musicLibrary.createFileAsync("audioCapture.mp3", Windows.Storage.CreationCollisionOption.generateUniqueName).done(function (newFile) {
                storageFile = newFile;
           oMediaCapture.startRecordToStorageFileAsync(profile, storageFile).done(function (file) {
                    // some code here
                }, function (err) {
                    WinJS.log && WinJS.log(err, "sample", "error");
                });
            });

    I also have created one additional function assigned to the separate button to stop recording:

    oMediaCapture.stopRecordAsync().done(function (result) {
                recordState = false;
    
                // Playback the recorded video.
                var audio = id("capturedAudio");
                audio.src = URL.createObjectURL(storageFile, { oneTimeOnly: true });
                audio.play();
                id("message").innerHTML = "Record Stopped.  File " + storageFile.path + "  ";
                localSettings.values[audioKey] = storageFile.path;
    
            },
            errorHandler);
    It doesn't work!

    Created file is empty as usual.

    Could you please help me or tell me what is wrong here?

    p.s.: the following Camera (Photo) Capture example works fine - http://code.msdn.microsoft.com/windowsapps/CameraCaptureUI-Sample-845a53ac

    Thanks in advance!

    Dmitry








    Tuesday, July 24, 2012 11:02 AM

All replies

  • Hi,

    Regard to your code, when starting the record, you’re calling:

    MediaCapture.startRecordToStorageFileAsync

    When stopping the record, you’re calling:

    oMediaCapture.stopRecordAsync

    So it looks like MediaCapture and oMediaCapture are two different objects. It is needed to use the same object in both start and stop methods.

    Best Regards,

    Ming Xu.


    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework

    Wednesday, July 25, 2012 11:47 AM
    Moderator
  • Hi,

    Thank you for your answer, but please have a look at the following code:

    // Create and initialze the MediaCapture object.
        function initMediaCapture() {
            oMediaCapture = null;
            oMediaCapture = new Windows.Media.Capture.MediaCapture();
            oMediaCapture.initializeAsync(captureInitSettings).then(function (result) {
                createProfile();
            });
        }

    and sorry, when starting the record I'm calling of course the correct one: oMediaCapture object. It was just a small misprint.


    Wednesday, July 25, 2012 1:41 PM
  • Dmitry,

    If I understand you correctly, This sample, scenario 3 does not work for you correct? http://code.msdn.microsoft.com/windowsapps/Media-Capture-Sample-adf87622/

    If that is true, It seems your camera is not supported.  What is the make and model of your camera?

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, July 25, 2012 2:37 PM
    Moderator