0

I was looking at the docs for the WASAPI ActivateAudioInterfaceAsync() function and it mentions passing DEVINTERFACE_AUDIO_CAPTURE as deviceInterfacePath to activate the interface on the default audio capture device. That seems like a good practice since the MediaDevice::GetDefaultAudioCaptureId(AudioDeviceRole::Default) call I would otherwise make to get the deviceInterfacePath parameter (which is used in the WASAPI sample) is synchronous - even though it may take a few seconds in some cases, blocking the UI thread and potentially getting your app to be killed.

Unfortunately the docs don't show a sample, especially for how to pass the GUID as the LPCWSTR deviceInterfacePath to ActivateAudioInterfaceAsync.

How can I do it?

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100

1 Answers1

2

What I managed to code up:

Includes to add:

#include <initguid.h>
#include <mmdeviceapi.h>

Initialization:

ComPtr<IActivateAudioInterfaceAsyncOperation> asyncOp; /*needed to get ActivateCompleted callback*/

PWSTR audioCaptureGuidString;
StringFromIID(DEVINTERFACE_AUDIO_CAPTURE, &audioCaptureGuidString);

// This call must be made on the main UI thread.  Async operation will call back to 
// IActivateAudioInterfaceCompletionHandler::ActivateCompleted
HRESULT hr = ActivateAudioInterfaceAsync(
    audioCaptureGuidString, /* deviceInterfacePath (default capture device) */
    __uuidof(IAudioClient2), /*riid*/
    nullptr, /*activationParams*/
    this, /*completionHandler*/
    &asyncOp /*createAsync*/);

CoTaskMemFree(audioCaptureGuidString);

// Windows holds a reference to the application's IActivateAudioInterfaceCompletionHandler interface
// until the operation is complete and the application releases the IActivateAudioInterfaceAsyncOperation interface
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • Well, nice try for a self answered question. The question itself is OT though in its current form. – πάντα ῥεῖ Aug 05 '16 at 17:54
  • See [here](http://meta.stackoverflow.com/a/330571/1413395) fro more information please. – πάντα ῥεῖ Aug 05 '16 at 18:14
  • πάντα ῥεῖ not sure what you mean. It's not a basic question that has an answer you can bingoogle since there's no sample showing how you do it. The answer might be easy to answer for someone well versed in the world of C++ and COM, but not everyone is an expert. I'm not asking the question to get points, but to help anyone else who'd be looking to find out how to do it since there's no sample for this invocation anywhere and there isn't easily searchable solution. Why don't you do something useful yourself instead of trolling people who are trying to help others? – Filip Skakun Aug 05 '16 at 21:39
  • 2
    Apparently whoever voted to close this question haven't tried researching the problem or is well versed in the technologies tagged. This question and answer provides real help in solving real problems that don't have a solution available in regular google searches. Try searching for `activateaudiointerfaceasync +"devinterface_audio_capture"` and all you find is the MSDN page, a random code dump and this question. It's easy to vote to close a question because you think the answer is obvious. It's harder to actually find the answer if you don't know it. – Filip Skakun Aug 08 '16 at 15:47
  • You may ask on meta stack overflow how you can improve the question part. – πάντα ῥεῖ Aug 08 '16 at 16:09