WASAPI: Choosing a wave format for exclusive output

心不动则不痛 提交于 2019-12-04 14:54:21

Well, I asked the MSDN forums and they came up with a good answer.

You need to check the device's default device format via IMMDevice::OpenPropertyStore(), and subsequently IPropertyStore::GetValue(), not IAudioClient::GetMixFormat(). Here is the code that retrieved an acceptable WAVEFORMATEX structure:

//CoInitialize/Enumerate devices

IPropertyStore* store = nullptr;

hr = device->OpenPropertyStore(STGM_READ, &store);

if (FAILED(hr)) {
    ExitProcess(1);
}

PROPVARIANT prop;

hr = store->GetValue(PKEY_AudioEngine_DeviceFormat, &prop);

if (FAILED(hr)) {
    ExitProcess(2);
}

hr = device->Activate (
    __uuidof(IAudioClient), 
    CLSCTX_ALL,
    NULL,
    (void**)&audioClient
);

device->Release();
device = nullptr;

if (FAILED(hr)) {
    ExitProcess(3);
}

hr = audioClient->IsFormatSupported (
    AUDCLNT_SHAREMODE_EXCLUSIVE,
    (PWAVEFORMATEX)prop.blob.pBlobData,
    NULL
);

if (FAILED(hr)) {
    ExitProcess(4);
}

The final value of hr is S_OK.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!