Windows Media Foundation 枚举音频设备
Posted
技术标签:
【中文标题】Windows Media Foundation 枚举音频设备【英文标题】:Windows Media Foundation Enumerate audio devices 【发布时间】:2017-02-28 06:16:12 【问题描述】:我使用 Microsoft MSDN 提供的教程来枚举音频设备。
这里是枚举代码:
HRESULT CreateAudioDeviceSource(IMFMediaSource **ppSource)
*ppSource = NULL;
IMFMediaSource *pSource = NULL;
IMFAttributes *pAttributes = NULL;
IMFActivate **ppDevices = NULL;
// Create an attribute store to specify the enumeration parameters.
HRESULT hr = MFCreateAttributes(&pAttributes, 1);
if (FAILED(hr))
goto done;
// Source type: audio capture devices
hr = pAttributes->SetGUID(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID
);
if (FAILED(hr))
goto done;
// Enumerate devices.
UINT32 count;
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
std::cout << count;
if (FAILED(hr))
std::cout << "Enum Failed";
goto done;
if (count == 0)
std::cout << "empty";
hr = E_FAIL;
goto done;
// Create the media source object.
hr = ppDevices[0]->ActivateObject(IID_PPV_ARGS(&pSource));
if (FAILED(hr))
goto done;
*ppSource = pSource;
(*ppSource)->AddRef();
done:
SafeRelease(&pAttributes);
for (DWORD i = 0; i < count; i++)
SafeRelease(&ppDevices[i]);
CoTaskMemFree(ppDevices);
SafeRelease(&pSource);
return hr;
但我未能调用该函数来枚举设备。我收到消息“枚举失败”。所以我不知道为什么会出现问题。
拜托,非常感谢!
参考:https://msdn.microsoft.com/en-us/library/dd317912(v=vs.85).aspx
【问题讨论】:
我想你应该用MFStartup
和MFShutdown
调用来包围这个教程片段。如果您报告失败,您还应该包含 hr
代码的值。
我会尝试你的建议。非常感谢。
【参考方案1】:
Windows Media Foundation 枚举音频设备的示例代码,设备捕获struct
struct CaptureDeviceParam
/// <summary>
/// The array of devices.
/// </summary>
IMFActivate **ppDevices;
/// <summary>
/// Device count.
/// </summary>
UINT32 count;
/// <summary>
/// Device selection.
/// </summary>
UINT32 selection;
;
还有enum
设备方法。
/// <summary>
/// Get the audio capture devices.
/// </summary>
/// <param name="param">The capture device param.</param>
void MediaCapture::GetAudioCaptureDevices(CaptureDeviceParam *param)
HRESULT hr = S_OK;
IMFAttributes *pAttributes = NULL;
// Initialize an attribute store to specify enumeration parameters.
hr = MFCreateAttributes(&pAttributes, 1);
// Ask for source type = audio capture devices
if (SUCCEEDED(hr))
// Set the device attribute.
hr = pAttributes->SetGUID(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_AUDCAP_GUID
);
// Enumerate devices.
if (SUCCEEDED(hr))
// Enumerate the device list.
hr = MFEnumDeviceSources(pAttributes, &(*param).ppDevices, &(*param).count);
// Safe release.
SafeRelease(&pAttributes);
GetAudioCaptureDevices
是'MediaCapture'类中的静态方法,可以随时调用。
【讨论】:
以上是关于Windows Media Foundation 枚举音频设备的主要内容,如果未能解决你的问题,请参考以下文章
Windows Media Foundation:获取 AAC 解码数据
Windows Media Foundation MFCreateSourceReaderFromURL 函数中的内存泄漏。任何替代功能?
在 Windows 10 上为 Qt 构建 WMF(Windows Media Foundation)媒体驱动程序插件
Microsoft Windows Server 2008 R2 中的 Microsoft Media Foundation
Windows 7 CoreAudio Media Foundation - IID_IAudioStreamVolume 的 uuidof