如何将DEVINTERFACE_AUDIO_CAPTURE传递给ActivateAudioInterfaceAsync?

时间:2016-08-05 17:42:30

标签: c++ com uwp guid wasapi

我正在查看WASAPI ActivateAudioInterfaceAsync()函数的文档,并提到将DEVINTERFACE_AUDIO_CAPTURE传递为deviceInterfacePath以激活默认音频捕获设备上的界面。这似乎是一个很好的做法,因为我会以MediaDevice::GetDefaultAudioCaptureId(AudioDeviceRole::Default)调用来获取deviceInterfacePath参数(在WASAPI示例中使用)是同步的 - 即使在某些情况下可能需要几秒钟,阻止UI线程并可能使您的应用程序被杀死。

不幸的是,文档不会显示示例,尤其是如何将GUID作为LPCWSTR deviceInterfacePath传递给ActivateAudioInterfaceAsync

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我设法编写代码:

包括添加:

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

初​​始化:

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