locked
Recording audio

    Question

  • Hello,
    I am trying to record an audio from the microphone and I am having some trouble accessing to the audio device controller. The code is pretty simple:

            private MediaCapture capture;

            public MainPage()
            {
                InitializeComponent();

                MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();
                settings.AudioDeviceId = "";
                settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
                settings.DeviceExtensionEnabled = true;
                settings.HardwareAccelerationEnabled = true;
               
                capture = new MediaCapture();
                capture.InitializeAsync(settings);
            }

            private void BtnListen_Click(object sender, RoutedEventArgs e)
            {
                var audioProperties = capture.AudioDeviceController;
            }

    When I click on the button I get the following exception:

    An exception of type 'System.Runtime.InteropServices.COMException' occurred in simplespeechrecognizer.exe but was not handled in user code

    Additional information: Exception from HRESULT: 0xC00D36B2

    I suspect I am not initializing the MediaCapture appropriately. Any hint?

    Thanks

    Friday, October 21, 2011 4:36 AM

Answers

  • The problem is that the MediaCapture object isn't getting initialized.  The call to InitializeAsync here is set up but doesn't actually occur.

    Async functions return an operation which needs to be started before it will occur.  The typical way to do this from C# is to await the operation by changing the line:

    capture.InitializeAsync(settings);
    
    

    to:

    await capture.InitializeAsync(settings);
    
    

    --Rob

    • Marked as answer by MsdnDev Tuesday, October 25, 2011 2:12 PM
    Tuesday, October 25, 2011 12:58 AM
    Owner

All replies

  • I am receiving the same error.  I'm looking into this.

    thanks

    Frank

    Monday, October 24, 2011 11:46 PM
    Moderator
  • The problem is that the MediaCapture object isn't getting initialized.  The call to InitializeAsync here is set up but doesn't actually occur.

    Async functions return an operation which needs to be started before it will occur.  The typical way to do this from C# is to await the operation by changing the line:

    capture.InitializeAsync(settings);
    
    

    to:

    await capture.InitializeAsync(settings);
    
    

    --Rob

    • Marked as answer by MsdnDev Tuesday, October 25, 2011 2:12 PM
    Tuesday, October 25, 2011 12:58 AM
    Owner
  • it didn't work and it saying "The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to"

    please help?!?!

    Wednesday, June 06, 2012 11:39 PM
  • As the error says, you need to add the "async" operator to your function's declaration to use await within it.

    In the future, please post new questions in new threads rather than adding them to an old answered thread. They are much more likely to be noticed promptly.

    --Rob

    Friday, July 20, 2012 11:44 AM
    Owner