1
我使用Microsoft MSDN提供的教程來枚舉音頻設備。Windows Media Foundation枚舉音頻設備
這是枚舉代碼:
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;
}
但我沒能調用函數枚舉設備。我收到消息「Enum Failed」。所以我不知道問題發生的原因。
請非常感謝!
參考:適用於Windows媒體基金會https://msdn.microsoft.com/en-us/library/dd317912(v=vs.85).aspx
我想你應該用['MFStartup'](https://msdn.microsoft.com/en-us/library/windows/desktop/ms702238)和'MFShutdown'調用將本教程片段包圍起來。如果你報告失敗,你還應該包括'hr'代碼的值。 –
我會嘗試你的建議。非常感謝。 – bcuzit