WASAPI:为独占输出选择波形格式 [英] WASAPI: Choosing a wave format for exclusive output

查看:1336
本文介绍了WASAPI:为独占输出选择波形格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用WASAPI打开具有输出设备的独占流。我无法选择可接受的格式,因为对于给定设备接受什么格式,似乎没有任何提示。

I'm trying to open an exclusive stream with an output device using WASAPI. I'm having trouble choosing an acceptable format, since there appear to be no hints as to what formats are accepted by a given device.

在我的情况下, IAudioClient :: GetMixFormat(),否则将返回设备的一种默认格式,返回不能在独占模式中使用的格式( IAudioClient :: IsFormatSupported()返回 AUDCLNT_E_UNSUPPORTED_FORMAT )。我不知道从那里去哪里。有一个可笑的数量的波形参数的组合 - 我真的必须迭代通过每一个,直到有效的工作?

In my case, IAudioClient::GetMixFormat(), which would otherwise return a sort of default format for the device, returns a format that can't be used in exclusive mode (IAudioClient::IsFormatSupported() returns AUDCLNT_E_UNSUPPORTED_FORMAT). I don't know where to go from there. There's a ridiculous number of combinations of wave format parameters - do I literally have to iterate through every one of them until something works?

推荐答案

您需要通过 IMMDevice检查设备的默认设备格式: :OpenPropertyStore(),然后 IPropertyStore :: GetValue(),而不是 IAudioClient :: GetMixFormat()。下面是检索到可接受的WAVEFORMATEX结构的代码:

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);
}

hr的最终值为S_OK。

The final value of hr is S_OK.

这篇关于WASAPI:为独占输出选择波形格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆