Windows Media Foundation枚举音频设备

时间:2017-02-28 06:16:13

标签: c++ ms-media-foundation

我使用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;
}

但我没有调用该函数来枚举设备。我收到消息“Enum Failed”。所以我不知道问题发生的原因。

拜托,非常感谢你!

参考:https://msdn.microsoft.com/en-us/library/dd317912(v=vs.85).aspx

1 个答案:

答案 0 :(得分: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'类中的静态方法,可以随时调用。