locked
how to use ActivateAudioInterfaceAsync to get IAudioClient interface?

    Question

  • In the new release of the system, old ActivateAudioInterface is not supported any more. My system is broken due to this change. I have been trying to use the new function ActivateAudioInterfaceAsync for two days but still cannot figure out how to use it correctly.

    I defined a class to implement IActivateAudioInterfaceCompletionHandler  interface and try to call ActivateAudioInterfaceAsync with an object of the type, but I got error "A method was called at an unexpected time".

    Is there a sample code I can learn from? I also don't understand the reason for the last parameter of ActivateAudioInterfaceAsync function since this is exactly same as the one passed into IActivateAudioInterfaceCompletionHandler::ActivateCompleted method. How are we supposed to use them?

    Any reply is appreciated. I am feeling very frustrated.


    • Edited by CA_Bay Tuesday, June 5, 2012 1:01 AM
    Tuesday, June 5, 2012 1:01 AM

Answers

  • Here is a link to the docs:  http://msdn.microsoft.com/en-us/library/windows/desktop/jj128298(v=vs.85).aspx

    The answers to your questions are in the remarks section:

    Remarks

    This function enables Metro style apps to  activate certain WASAPI COM interfaces after using Windows Runtime APIs in the Windows.Devices and Windows.Media.Devices namespaces to select an audio device. 

    An application must call this function from the main UI thread to activate a COM interface in the WASAPI family. The application passes an IActivateAudioInterfaceCompletionHandler callback COM interface through completionHandler. Windows calls a method in the application’s IActivateAudioInterfaceCompletionHandler interface from a worker thread in the COM Multi-threaded Apartment (MTA) when the activation results are available. The application can then call a method in the IActivateAudioInterfaceAsyncOperation interface  to retrieve the result code and the requested WASAPI interface.

    Windows holds a reference to the application's IActivateAudioInterfaceCompletionHandler interface until the operation is complete and the application releases the IActivateAudioInterfaceAsyncOperation interface.

    Depending on which WASAPI interface is activated, this function may display a consent prompt the first time it is called. For example, when the application calls this function to activate IAudioClient to access a microphone, the purpose of the consent prompt is to get the user's permission for the app to access the microphone. For more information about the consent prompt, see Guidelines for using sensitive devices.

    ActivateAudioInterfaceAsync must be called on the main UI thread so that the consent prompt can be shown. If the consent prompt can’t be shown, the user can’t grant device access to the app.

    ActivateAudioInterfaceAsync must be called on a thread in a COM Single-Threaded Apartment (STA). Calls from a thread on the COM MTA will result in an error code of E_ILLEGAL_METHOD_CALL.

    Tuesday, June 12, 2012 7:44 PM
    Moderator
  • Thank You Tom!
    Friday, August 10, 2012 4:26 PM
    Moderator
  • Incidentally, I noticed that if my IActivateAudioInterfaceCompletionHandler-implementing object fails to also implement IAgileObject, then I get E_ILLEGAL_METHOD_CALL (0x8000000E), "a method was called at an unexpected time".

    Solution: make sure I also implement IAgileObject in my callback-handling object. (incidentally, if I implement this, then Windows doesn't seem to even bother checking my IMarshal interface.)

    Saturday, September 8, 2012 5:21 PM

All replies

  • CA_Bay,

    The following sample should be helpful:

    http://code.msdn.microsoft.com/windowsapps/Audio-hardware-offload-280dc923

    Best Wishes - Eric

    Tuesday, June 5, 2012 4:18 AM
    Moderator
  • Eric,

    Thank you very much for your information. It does help.

    From the sample code up there, the last parameter of ActivateAudioInterfaceAsync is not used at all. Is there a reason for it to be there at all?

    Two more questions:

    1. The document says ActivateAudioInterfaceAsync should be called in main UI thread. Can you explain a little more about this? And the reason behind this? I have c# app using c++ component. I don't think this function can be called from my c# main UI thread. Is there a problem of this?

    2. IAudioClient->Initialize function hang sometime. To avoid my app hang due to this problem, I tried to call this function in a separate thread and set a timer so that when it times out, the thread will be killed. Is there a fix for this problem? Same for Windows::Media::Devices::MediaDevice::GetDefaultAudioCaptureId. I haven't got a chance to test this function in new release. Hopefully this is fixed in the new release.

    Thank you very much,




    • Edited by CA_Bay Tuesday, June 5, 2012 9:33 PM
    Tuesday, June 5, 2012 6:48 PM
  • Eric,

    Thank you very much for your information. It does help.

    From the sample code up there, the last parameter of ActivateAudioInterfaceAsync is not used at all. Is there a reason for it to be there at all?

    Two more questions:

    1. The document says ActivateAudioInterfaceAsync should be called in main UI thread. Can you explain a little more about this? And the reason behind this? I have c# app using c++ component. I don't think this function can be called from my c# main UI thread. Is there a problem of this?

    2. IAudioClient->Initialize function hang sometime. To avoid my app hang due to this problem, I tried to call this function in a separate thread and set a timer so that when it times out, the thread will be killed. Is there a fix for this problem? Same for Windows::Media::Devices::MediaDevice::GetDefaultAudioCaptureId. I haven't got a chance to test this function in new release. Hopefully this is fixed in the new release.

    Thank you very much,




    Note  In Windows 8 Release Preview, the first use of IAudioClient to access the audio device should be on the STA thread. Calls from an MTA thread may result in undefined behavior.

    Do you have a link to the docs for  ActivateAudioInterfaceAsync?

    Best Wishes - Eric

    Friday, June 8, 2012 7:37 AM
    Moderator
  • Here is a link to the docs:  http://msdn.microsoft.com/en-us/library/windows/desktop/jj128298(v=vs.85).aspx

    The answers to your questions are in the remarks section:

    Remarks

    This function enables Metro style apps to  activate certain WASAPI COM interfaces after using Windows Runtime APIs in the Windows.Devices and Windows.Media.Devices namespaces to select an audio device. 

    An application must call this function from the main UI thread to activate a COM interface in the WASAPI family. The application passes an IActivateAudioInterfaceCompletionHandler callback COM interface through completionHandler. Windows calls a method in the application’s IActivateAudioInterfaceCompletionHandler interface from a worker thread in the COM Multi-threaded Apartment (MTA) when the activation results are available. The application can then call a method in the IActivateAudioInterfaceAsyncOperation interface  to retrieve the result code and the requested WASAPI interface.

    Windows holds a reference to the application's IActivateAudioInterfaceCompletionHandler interface until the operation is complete and the application releases the IActivateAudioInterfaceAsyncOperation interface.

    Depending on which WASAPI interface is activated, this function may display a consent prompt the first time it is called. For example, when the application calls this function to activate IAudioClient to access a microphone, the purpose of the consent prompt is to get the user's permission for the app to access the microphone. For more information about the consent prompt, see Guidelines for using sensitive devices.

    ActivateAudioInterfaceAsync must be called on the main UI thread so that the consent prompt can be shown. If the consent prompt can’t be shown, the user can’t grant device access to the app.

    ActivateAudioInterfaceAsync must be called on a thread in a COM Single-Threaded Apartment (STA). Calls from a thread on the COM MTA will result in an error code of E_ILLEGAL_METHOD_CALL.

    Tuesday, June 12, 2012 7:44 PM
    Moderator
  • Trying to finally port my ancient CP component into RP, I'm hitting the illegal method call error. How can I make sure the C# app I'm calling from is STA? Putting [STAThread] on the method accessing the code doesn't do anything.
    Sunday, July 8, 2012 5:07 PM
  • Tom Servo,

    Were you able to get this resolved?

    Best Wishes - Eric

    Friday, August 3, 2012 12:39 PM
    Moderator
  • Yeah, a while ago. I couldn't exactly tell you anymore what the problem was, tho. IIRC it was something trivial.
    Friday, August 10, 2012 3:43 PM
  • Thank You Tom!
    Friday, August 10, 2012 4:26 PM
    Moderator
  • Incidentally, I noticed that if my IActivateAudioInterfaceCompletionHandler-implementing object fails to also implement IAgileObject, then I get E_ILLEGAL_METHOD_CALL (0x8000000E), "a method was called at an unexpected time".

    Solution: make sure I also implement IAgileObject in my callback-handling object. (incidentally, if I implement this, then Windows doesn't seem to even bother checking my IMarshal interface.)

    Saturday, September 8, 2012 5:21 PM