如何在MS Windows 7中获取音频设备的Jack信息

德米特里·博伊科

我需要使用C#,C ++或任何.NET框架获取MS Windows 7中音频设备杰克信息

我检查了NAudio框架,但在这种情况下似乎无法使用它。同样,这些链接也没有用。https : //msdn.microsoft.com/zh-cn/library/windows/desktop/dd370793( v = vs.85).aspxhttps://msdn.microsoft.com/zh-CN /library/windows/desktop/dd370812(v=vs.85).aspx

而且这种方法也无济于事。

ManagementObjectSearcher objSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_SoundDevice");

            ManagementObjectCollection objCollection = objSearcher.Get();

            foreach (ManagementObject obj in objCollection)
            {
                foreach (PropertyData property in obj.Properties)
                {
                    Debug.WriteLine(String.Format("{0}:{1}", property.Name, property.Value));
                }
            }

在此处输入图片说明

关于如何获取此信息或至少对该属性有一些MSDN引用的任何线索?

PS这是C ++代码,用于掩盖我使用的音频设备的相似属性,但无法获取如何获取杰克信息

#pragma once
#include "Mmdeviceapi.h"
#include "PolicyConfig.h"
#include "Propidl.h"
#include "NotificationClient.h"
#include "AudioDevice.h"

namespace AudioDeviceUtil {

    ref class MmDeviceApiWrapper
    {
    public:
        MmDeviceApiWrapper(void)
        {
            //.Net threads are coinitialized...
            //CoInitializeEx(NULL, COINIT_MULTITHREADED);
            notificationRegistered = false;
            audioDeviceNotificationHelper = gcnew AudioDeviceNotificationHelper();
            pNotifyClient = NULL;

        }

        virtual ~MmDeviceApiWrapper(void)
        {
            //CoUninitialize();
            if (notificationRegistered)
                RegisterForNotification(false);
        }

        static property AudioDeviceNotificationHelper^ AudioDeviceNotification
        {
            AudioDeviceNotificationHelper^ get()
            {
                return audioDeviceNotificationHelper;
            }
        };

        static property bool IsRegisteredForNotification
        {
            bool get()
            {
                return notificationRegistered;
            }
        }

        // Enumerates playback device list and marks the default device by the appropriate flag
        static System::Collections::Generic::List<AudioDevice^>^ GetPlaybackDeviceList() 
        {
            System::Collections::Generic::List<AudioDevice^>^ playbackDevices = 
                gcnew System::Collections::Generic::List<AudioDevice^>();

            HRESULT hr = S_OK;//CoInitialize(NULL);
            HRESULT hr2 = S_OK; 
            //HRESULT hr3 = S_OK; 

            if (SUCCEEDED(hr))
            {
                IMMDeviceEnumerator *pEnum = NULL;
                // Create a multimedia device enumerator.
                hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,                
                    CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnum);
                if (SUCCEEDED(hr))
                {
                    IMMDevice *pDevice;
                    IMMDeviceCollection *pDevices;
                    LPWSTR wstrDefaultID = L"";
                    // Enumerate the output devices.
                    hr = pEnum->EnumAudioEndpoints(eRender, 
                        DEVICE_STATE_ACTIVE | DEVICE_STATE_UNPLUGGED | DEVICE_STATE_DISABLED, &pDevices);
                    if (SUCCEEDED(hr))
                    {
                        HRESULT hrDef = pEnum->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
                        if (SUCCEEDED(hrDef))
                        {
                            hrDef = pDevice->GetId(&wstrDefaultID);
                        }
                        System::Diagnostics::Trace::WriteLineIf(!SUCCEEDED(hrDef),
                            System::String::Format("[MmDeviceApiWrapper] GetDefaultAudioEndPoint failed: {0:X}", hr), "Error");
                    }

                    if (SUCCEEDED(hr))
                    {                                                  
                        UINT count;
                        pDevices->GetCount(&count);
                        if (SUCCEEDED(hr))
                        {
                            for (unsigned int i = 0; i < count; i++)
                            {
                                hr = pDevices->Item(i, &pDevice);
                                if (SUCCEEDED(hr))
                                {
                                    LPWSTR wstrID = NULL;
                                    DWORD dwState = 0;
                                    hr = pDevice->GetId(&wstrID);
                                    hr2 = pDevice->GetState(&dwState);
                                    if (SUCCEEDED(hr) && SUCCEEDED(hr2))
                                    {
                                        IPropertyStore *pStore;
                                        hr = pDevice->OpenPropertyStore(STGM_READ, &pStore);
                                        if (SUCCEEDED(hr))
                                        {

                                            //PROPVARIANT jackSubType;
                                            //PropVariantInit(&jackSubType);
                                            //hr3 = pStore->GetValue(PKEY_Device_JackSubType, &jackSubType);

                                            //
                                            PROPVARIANT friendlyName;
                                            PropVariantInit(&friendlyName);
                                            hr = pStore->GetValue(PKEY_Device_FriendlyName, &friendlyName);
                                            if (SUCCEEDED(hr))
                                            {
                                                System::String^ name = gcnew System::String(friendlyName.pwszVal);
                                                playbackDevices->Add(gcnew AudioDevice(gcnew System::String(wstrID), 
                                                    name, (AudioDeviceStateType)dwState, 0 == wcscmp(wstrID, wstrDefaultID)));
                                                PropVariantClear(&friendlyName);
                                            }
                                            /*if (SUCCEEDED(hr3))
                                            {
                                                  PropVariantClear(&jackSubType);
                                            }*/
                                            pStore->Release();
                                        }
                                    }
                                    System::Diagnostics::Trace::WriteLineIf(!(SUCCEEDED(hr) && SUCCEEDED(hr2)),
                                        System::String::Format("[MmDeviceApiWrapper] GetID or GetState failed: {0:X}", hr), "Error");
                                    pDevice->Release();
                                }
                            }
                        }
                        pDevices->Release();
                    }
                    pEnum->Release();
                }
            }
            System::Diagnostics::Trace::WriteLineIf(!(SUCCEEDED(hr) && SUCCEEDED(hr2)),
                System::String::Format("[MmDeviceApiWrapper] Error: GetPlaybackDeviceList failed: {0:X}, {1:X}", hr, hr2), "Error");

            //CoUninitialize();
            return playbackDevices;
        }

        // Get default playback device on the system 
        static AudioDevice^ GetDefaultPlaybackDevice()
        {
            AudioDevice^ defaultPlaybackDevice = nullptr;
            HRESULT hr = S_OK;//CoInitialize(NULL);
            //HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
            HRESULT hr2 = S_OK;
            if (SUCCEEDED(hr))
            {
                IMMDeviceEnumerator *pEnum = NULL;
                // Create a multimedia device enumerator.
                hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, 
                    CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnum);
                if (SUCCEEDED(hr))
                {
                    IMMDevice *pDevice;
                    // Enumerate the output devices.
                    hr = pEnum->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
                    LPWSTR wstrID = NULL;
                    hr = pDevice->GetId(&wstrID);
                    DWORD dwState = 0;
                    hr2 = pDevice->GetState(&dwState);
                    if (SUCCEEDED(hr) && SUCCEEDED(hr2))
                    {
                        IPropertyStore *pStore;
                        hr = pDevice->OpenPropertyStore(STGM_READ, &pStore);
                        if (SUCCEEDED(hr))
                        {
                            PROPVARIANT friendlyName;
                            PropVariantInit(&friendlyName);
                            hr = pStore->GetValue(PKEY_Device_FriendlyName, &friendlyName);
                            if (SUCCEEDED(hr))
                            {
                                defaultPlaybackDevice = gcnew AudioDevice(
                                gcnew System::String(friendlyName.pwszVal), gcnew System::String(wstrID), 
                                (AudioDeviceStateType)dwState, true);
                            }
                            PropVariantClear(&friendlyName);
                        }
                        pStore->Release();
                    }
                    pDevice->Release();
                }
            }
            System::Diagnostics::Trace::WriteLineIf(!(SUCCEEDED(hr) && SUCCEEDED(hr2)),
                System::String::Format("[MmDeviceApiWrapper] Error: GetDefaultPlaybackDevice failed: {0:X}, {1:X}", hr, hr2), "Error");

            //CoUninitialize();
            return defaultPlaybackDevice;
        }

        // Set default playback device on the system
        // returns true if succeeded.
        static bool SetDefaultPlaybackDevice(LPCWSTR devIDString)
        {   
            IPolicyConfigVista *pPolicyConfig;
            ERole reserved = eConsole;

            HRESULT hr = CoCreateInstance(__uuidof(CPolicyConfigVistaClient), 
                NULL, CLSCTX_ALL, __uuidof(IPolicyConfigVista), (LPVOID *)&pPolicyConfig);

            System::Diagnostics::Trace::WriteLineIf(!SUCCEEDED(hr),
                System::String::Format("[MmDeviceApiWrapper] SetDefaultPlaybackDevice CoCreate failed: {0:X}", hr), "Error");

            if (SUCCEEDED(hr))
            {
                System::Diagnostics::Trace::WriteLine(
                    System::String::Format("[MmDeviceApiWrapper] SetDefaultPlaybackDevice to devId {0}", gcnew System::String(devIDString)), "Information");

                hr = pPolicyConfig->SetDefaultEndpoint(devIDString, reserved);

                System::Diagnostics::Trace::WriteLineIf(SUCCEEDED(hr),
                    System::String::Format("[MmDeviceApiWrapper] SetDefaultPlaybackDevice SetDefEndPoint succeeded."), "Information");
                System::Diagnostics::Trace::WriteLineIf(!SUCCEEDED(hr),
                    System::String::Format("[MmDeviceApiWrapper] SetDefaultPlaybackDevice SetDefEndPoint failed: {0:X}", hr), "Error");

                pPolicyConfig->Release();
            }
            return SUCCEEDED(hr);
        }

        static bool RegisterForNotification()
        {
            if(!notificationRegistered)
            {
                pNotifyClient = new CMMNotificationClient(audioDeviceNotificationHelper);
                notificationRegistered = RegisterForNotification(true);
            }
            return notificationRegistered;
        }

        static bool UnRegisterForNotification()
        {
            if(notificationRegistered && pNotifyClient)
            {
                notificationRegistered = !RegisterForNotification(false);
                SAFE_DELETE(pNotifyClient);
                return notificationRegistered;
            }
            else
            {
                return false;
            }
        }

    private:

        static AudioDeviceNotificationHelper^ audioDeviceNotificationHelper;
        static bool notificationRegistered;
        static CMMNotificationClient* pNotifyClient;

        // Registered or unregister for notification.
        // The clients can use the event of the A 
        // returns true if succeeded.
        static bool RegisterForNotification(bool registerForNotification)
        {
            HRESULT hr = S_OK;//CoInitialize(NULL);
            if (SUCCEEDED(hr))
            {                                                                
                IMMDeviceEnumerator *pEnum = NULL;
                // Create a multimedia device enumerator.
                hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,
                    CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnum);
                if (SUCCEEDED(hr))
                {
                    IMMNotificationClient* pNotify = (IMMNotificationClient*)(pNotifyClient);
                    if(!registerForNotification)
                    {
                        hr = pEnum->UnregisterEndpointNotificationCallback(pNotify);
                    }
                    else
                    {                                                         
                        hr = pEnum->RegisterEndpointNotificationCallback(pNotify);
                    } 
                    System::Diagnostics::Trace::WriteLineIf(SUCCEEDED(hr),
                        System::String::Format("[MmDeviceApiWrapper] {0}Register for notification succeeded.", 
                        registerForNotification ? "" : "Un" ), "Information");
                    System::Diagnostics::Trace::WriteLineIf(!SUCCEEDED(hr),
                        System::String::Format("[MmDeviceApiWrapper] Error: {0}Register for notification not succeded! Code: {1}", 
                        registerForNotification ? "" : "Un" ,
                        (hr == E_POINTER ? "E_POINTER" : 
                        (hr == E_OUTOFMEMORY ? "E_OUTOFMEMORY" : 
                        (hr == E_NOTFOUND ? "E_NOTFOUND" : "NOT_DEFINED")))), "Error");
                }
                pEnum->Release();
            }
            //CoUninitialize();
            return SUCCEEDED(hr);
        }

    };
}
西蒙·莫里尔

要获取插孔信息,您需要使用DeviceTopology API

该API定义了一个IKsJackDescription接口接口将为您提供插孔连接器信息。这是一个示例控制台应用程序,可在计算机上为所有渲染设备显示此信息:

int main()
{
    HRESULT hr = S_OK;
    CoInitialize(NULL);

    CComPtr<IMMDeviceEnumerator> enumerator;
    hr = enumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
    if (SUCCEEDED(hr))
    {
        CComPtr<IMMDeviceCollection> devices;
        hr = enumerator->EnumAudioEndpoints(EDataFlow::eRender, DEVICE_STATEMASK_ALL, &devices);
        if (SUCCEEDED(hr))
        {
            UINT count = 0;
            devices->GetCount(&count);
            for (int i = 0; i < count; i++)
            {
                CComPtr<IMMDevice> device;
                hr = devices->Item(i, &device);
                if (SUCCEEDED(hr))
                {
                    CComPtr<IPropertyStore> store;
                    hr = device->OpenPropertyStore(STGM_READ, &store);
                    if (SUCCEEDED(hr))
                    {
                        PROPVARIANT pv;
                        PropVariantInit(&pv);
                        hr = store->GetValue(PKEY_Device_FriendlyName, &pv);
                        if (SUCCEEDED(hr))
                        {
                            PWSTR p;
                            PSFormatForDisplayAlloc(PKEY_Device_FriendlyName, pv, PDFF_DEFAULT, &p);
                            wprintf(L"name: '%s'\n", p);
                            CoTaskMemFree(p);
                        }
                        PropVariantClear(&pv);
                    }

                    CComPtr<IDeviceTopology> topology;
                    hr = device->Activate(__uuidof(IDeviceTopology), CLSCTX_ALL, NULL, (void**)&topology);
                    if (SUCCEEDED(hr))
                    {
                        CComPtr<IConnector> connector;
                        hr = topology->GetConnector(0, &connector);
                        if (SUCCEEDED(hr))
                        {
                            CComPtr<IConnector> connectedTo;
                            hr = connector->GetConnectedTo(&connectedTo);
                            if (SUCCEEDED(hr))
                            {
                                CComPtr<IPart> part;
                                hr = connectedTo->QueryInterface(&part);
                                if (SUCCEEDED(hr))
                                {
                                    CComPtr<IKsJackDescription> jack;
                                    hr = part->Activate(CLSCTX_ALL, IID_PPV_ARGS(&jack));
                                    if (SUCCEEDED(hr))
                                    {
                                        UINT jackCount = 0;
                                        jack->GetJackCount(&jackCount);
                                        for (int j = 0; j < jackCount; j++)
                                        {
                                            KSJACK_DESCRIPTION desc = { 0 };
                                            jack->GetJackDescription(j, &desc);
                                            wprintf(L" jack[%i] channel mapping: %i\n", j, desc.ChannelMapping);
                                            wprintf(L" jack[%i] connection type: %i\n", j, desc.ConnectionType);
                                            wprintf(L" jack[%i] is connected: %i\n", j, desc.IsConnected);
                                            wprintf(L" jack[%i] color: 0x%08X\n", j, desc.Color);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    enumerator.Release();
    CoUninitialize();
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Windows 7上同时使用两个音频设备?

当Windows 7报告“音频设备已禁用”但不是时如何处理?

如何在Windows 8中将不同的音频设备用于不同的应用程序?

Windows Shell:如何获得音频设备的名称?

如何从Windows 10摆脱显示器音频设备

如何停止Windows 10重命名音频设备?

如何在Windows 7上运行MS C ++ 6.0

如何在同一音频设备上同时使用JACK和Pulseaudio / ALSA?

如何在Windows的MS Outlook中查看MS To-Do的“我的一天”任务

如何在Windows中以30ms为间隔执行10ms CPU时间操作?

我如何让Windows 10假设3.5mm音频设备是耳机?

Corsair iCue-编程鼠标按钮以启动.BAT文件以更改Windows 7中的默认音频设备-无法启动

如何在VBScript中在MS Windows 7 64位下引用WIN32对象

如何识别Powershell中的默认音频设备?

如何在 Windows 中获取系统音量级别(音频)?

如何在MS Windows 7英文下配置西班牙语语音

增加USB音频设备上的音量/功率级别(64位Windows 7)

如何在Windows终端中更改标题栏颜色(从ms store安装)

如何在MS Windows中更改“本地系统”帐户的%PATH%值?

如何在工作组环境中为MS SQL Server创建Windows服务帐户?

如何在MS Windows上安装CVXPY?

如何在Windows中获取额外的Nvidia GPU信息?

如何获取物理设备(WinAPI,Windows)支持的音频格式

如何在Windows Vista / 7上管理多个音频播放设备?

如何在Windows 10和> .net 4.5中的C#中枚举音频输入设备?

如何在单独的音轨上输入多个音频设备

如何换回音频设备?

如何选择最佳的音频设备

如何更改默认音频设备端口?