Core Audio 枚举电脑上音频设备.

代码来源:    https://github.com/skitaoka/windows-core-audio-api-samples  

默认配置是 VS2015编译,如果没有安装,编译会失败,

通过  项目右键->属性->Configuration Properties->General->Platform Toolset 修改 当前已经安装的 平台.  vs2013 (v120).


#include <memory>
#include <sstream>

#include <wrl.h>
#include <propvarutil.h>
#include <mmdeviceapi.h>
#include <functiondiscoverykeys_devpkey.h>

namespace {

using prop_string_t = std::unique_ptr<WCHAR, decltype(&CoTaskMemFree)>;

#define prop_string_null prop_string_t(nullptr, CoTaskMemFree)

prop_string_t GetDeviceId(IN Microsoft::WRL::ComPtr<IMMDevice> const& device) {
  LPWSTR buffer = nullptr;
  if SUCCEEDED(device->GetId(&buffer)) {
    return prop_string_t(buffer, CoTaskMemFree);
  }
  return prop_string_null;
}

std::wstring GetDeviceState(IN Microsoft::WRL::ComPtr<IMMDevice> const& device) {
  DWORD state = 0;
  if SUCCEEDED(device->GetState(&state)) {
    switch (state) {
    case DEVICE_STATE_ACTIVE: return L"ACTIVE";
    case DEVICE_STATE_DISABLED: return L"DISABLED";
    case DEVICE_STATE_NOTPRESENT: return L"NOTPRESENT";
    case DEVICE_STATE_UNPLUGGED: return L"UNPLUGGED";
    }
  }
  return L"UNKNOWN";
}

prop_string_t GetPropString(IN Microsoft::WRL::ComPtr<IPropertyStore> const& prop,
                            IN PROPERTYKEY const& key) {
  PROPVARIANT var;
  ::PropVariantInit(&var);
  if SUCCEEDED(prop->GetValue(key, &var)) {
    LPWSTR str;
    if SUCCEEDED(::PropVariantToStringAlloc(var, &str)) {
      ::PropVariantClear(&var);
      return prop_string_t(str, CoTaskMemFree);
    }
  }
  ::PropVariantClear(&var);
  return prop_string_null;
}

} // end of anonymous namespace

int main() {
  if SUCCEEDED(::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED)) {
    {
      Microsoft::WRL::ComPtr<IMMDeviceEnumerator> enumerator;
      if SUCCEEDED(::CoCreateInstance(__uuidof(MMDeviceEnumerator),
                                      nullptr,
                                      CLSCTX_INPROC_SERVER,
                                      IID_PPV_ARGS(enumerator.ReleaseAndGetAddressOf()))) {

        // 出力先デバイスを列挙
        Microsoft::WRL::ComPtr<IMMDeviceCollection> devices;
        if SUCCEEDED(enumerator->EnumAudioEndpoints(EDataFlow::eAll, // eRender, eCapture, eAll
                                                    DEVICE_STATEMASK_ALL,
                                                    // DEVICE_STATE_ACTIVE: 有効化されている
                                                    // DEVICE_STATE_DISABLED: 無効化されている
                                                    // DEVICE_STATE_NOTPRESENT: システムに無登録
                                                    // DEVICE_STATE_UNPLUGGED: 物理的に接続されていない
                                                    devices.ReleaseAndGetAddressOf())) {
          std::wostringstream out;

          // デバイス数を取得
          UINT uNumDevices;
          if SUCCEEDED(devices->GetCount(&uNumDevices)) {
            for (UINT i = 0; i < uNumDevices; ++i) {
              // デバイスを取得
              Microsoft::WRL::ComPtr<IMMDevice> device;
              if SUCCEEDED(devices->Item(i, device.ReleaseAndGetAddressOf())) {
                // デバイスの情報を取得
                auto const id = GetDeviceId(device); // デバイスID
                auto const state = GetDeviceState(device); // デバイスの状態

                Microsoft::WRL::ComPtr<IMMEndpoint> endpoint;
                device.As<IMMEndpoint>(&endpoint);

                Microsoft::WRL::ComPtr<IPropertyStore> prop;
                if SUCCEEDED(device->OpenPropertyStore(STGM_READ, prop.ReleaseAndGetAddressOf())) {
                  auto const name = GetPropString(prop, PKEY_Device_FriendlyName); // フルネーム
                  auto const desc = GetPropString(prop, PKEY_Device_DeviceDesc); // ショートネーム
                  auto const audioif = GetPropString(prop, PKEY_DeviceInterface_FriendlyName); // 物理デバイス名
                  out << L"[" << i << L"] id=" << id.get() << L", state=" << state << L", name=" << name.get()
                    << L", desc=" << desc.get() << L", audioif=" << audioif.get() << L'\n';
                }
              }
            }
          }

          std::wstring const names = out.str();
          ::MessageBoxW(HWND_DESKTOP, names.c_str(), L"devices", MB_OK);
        }
      }
    }
    ::CoUninitialize();
  }

  return 0;
}

输出结果如下:



音视频交流欢迎+ 群:

210324637


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值