Winapi:通过音频设备枚举获取音频设备ID

时间:2019-07-08 14:06:58

标签: c++ windows winapi audio

继续this答案:

任何人,请发布音频设备枚举示例代码。

1 个答案:

答案 0 :(得分:1)

这是一个简短的示例,说明如何使用旧的Waveform Audio界面列出音频设备。

//#define UNICODE
#include <Windows.h>

#include <iostream>
#include <vector>

// WAVEOUTCAPS with the device id as an added member
struct WaveOutCaps_t : WAVEOUTCAPS {
    WaveOutCaps_t(UINT id) : WAVEOUTCAPS{ 0 }, DeviceId{ id } {}
    UINT DeviceId;
    operator WAVEOUTCAPS* () { return this; }
};

class WaveOutDevices {
public:
    WaveOutDevices() {
        Refresh();
    }
    void Refresh() {
        Caps.clear();
        UINT devs = waveOutGetNumDevs(); // get the number of devices
        for (UINT did = 0; did < devs; ++did) {
            WaveOutCaps_t caps(did);
            // query for the capabilities of the device   
            if (waveOutGetDevCaps(did, caps, sizeof(WAVEOUTCAPS)) == MMSYSERR_NOERROR)
                Caps.push_back(caps); // store the result
        }
    }
//private:
    std::vector<WaveOutCaps_t> Caps;
};

int main() {
    WaveOutDevices wod;
    // list device ids and names
    for (const auto& dev : wod.Caps)
        std::wcout << L"DeviceId: " << dev.DeviceId << L" Name: " << dev.szPname << L"\n";
}