如何以编程方式为应用程序设置默认的输入和输出音频设备? [英] How can I programmatically set the default input and output audio device for an application?

查看:53
本文介绍了如何以编程方式为应用程序设置默认的输入和输出音频设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我转到Windows 10(1803)计算机上的设置",则可以访问页面(应用程序音量和设备首选项"),该页面可以让我为正在运行的应用程序设置默认的输入和输出设备.

If I go to Settings on a Windows 10 (1803) computer, I have access to a page ("App Volume and Device Preferences") that lets me set the default input and output device for a running application.

如何通过编程方式设置这些选项?

How can I set these options programmatically?

相关:

推荐答案

在这里您可以枚举所有播放设备

Here you can enumerate all the playback devices

#include <windows.h>
#include <mmsystem.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "Winmm.lib")

int main()
{
    int nSoundCardCount = waveOutGetNumDevs();

    for (int i = 0; i < nSoundCardCount; i++)
    {
        WAVEOUTCAPS woc;
        waveOutGetDevCaps(i, &woc, sizeof(woc));

        cout << woc.szPname << endl; 
    }

    system("pause");
    return 0;
}


在这里,您需要使用PolicyConfig.h和SetDefaultAudioPlaybackDevice来添加.h文件和接口.请参考此项目

1.添加头文件 PolicyConfig.h

2.添加头文件和界面.

2.Add the head file and interface.

#include "Mmdeviceapi.h"
#include "PolicyConfig.h"
#include "Propidl.h"
#include "Functiondiscoverykeys_devpkey.h"

HRESULT SetDefaultAudioPlaybackDevice( LPCWSTR devID )
{
    IPolicyConfigVista *pPolicyConfig;
    ERole reserved = eConsole;

    HRESULT hr = CoCreateInstance(__uuidof(CPolicyConfigVistaClient), 
        NULL, CLSCTX_ALL, __uuidof(IPolicyConfigVista), (LPVOID *)&pPolicyConfig);
    if (SUCCEEDED(hr))
    {
        hr = pPolicyConfig->SetDefaultEndpoint(devID, reserved);
        pPolicyConfig->Release();
    }
    return hr;
}

3.使用上面的界面编写用于设置默认输出设备的函数.

3.Use the above interface to write a function to set the default output device.

这是MFC项目.也许你需要改变.

It's MFC Project. Maybe you need to change.

需要设置哪个输出设备,您可以自己修改宏的内容.我使用 waveOutGetDevCaps()

Which output device needs to be set, you can modify the content of the macro yourself. I get the name of output device using waveOutGetDevCaps()

//Set the default audio playback device 
#define  DEF_AUDIO_NAME _T("Speakers (2- Logitech USB Heads")  //modify it, my device is Speakers (2- Logitech USB Heads

void InitDefaultAudioDevice()
{
    HRESULT hr = 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))
        {
            //Determine if it is the default audio device
            bool bExit = false;
            IMMDevice  *pDefDevice = NULL;
            hr = pEnum->GetDefaultAudioEndpoint(eRender, eMultimedia,&pDefDevice);
            if (SUCCEEDED(hr))
            {
                IPropertyStore *pStore;
                hr = pDefDevice->OpenPropertyStore(STGM_READ, &pStore);
                if (SUCCEEDED(hr))
                {
                    PROPVARIANT friendlyName;
                    PropVariantInit(&friendlyName);
                    hr = pStore->GetValue(PKEY_Device_FriendlyName, &friendlyName);
                    if (SUCCEEDED(hr))
                    {
                        CString strTmp = friendlyName.pwszVal;
                        if (strTmp.Find(DEF_AUDIO_NAME) != -1)
                        {
                            bExit = true;
                        }
                        PropVariantClear(&friendlyName);
                    }
                    pStore->Release();
                }
                pDefDevice->Release();
            }
            if (bExit)
            {
                pEnum->Release();
                return;
            }

            IMMDeviceCollection *pDevices;
            // Enumerate the output devices.
            hr = pEnum->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pDevices);
            if (SUCCEEDED(hr))
            {
                UINT count;
                pDevices->GetCount(&count);
                if (SUCCEEDED(hr))
                {
                    for (int i = 0; i < count; i++)
                    {
                        bool bFind = false;
                        IMMDevice *pDevice;
                        hr = pDevices->Item(i, &pDevice);
                        if (SUCCEEDED(hr))
                        {
                            LPWSTR wstrID = NULL;
                            hr = pDevice->GetId(&wstrID);
                            if (SUCCEEDED(hr))
                            {
                                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))
                                    {
                                        // if no options, print the device
                                        // otherwise, find the selected device and set it to be default
                                        CString strTmp = friendlyName.pwszVal;
                                        if (strTmp.Find(DEF_AUDIO_NAME) != -1)
                                        {
                                            SetDefaultAudioPlaybackDevice(wstrID);
                                            bFind = true;
                                        }
                                        PropVariantClear(&friendlyName);
                                    }
                                    pStore->Release();
                                }
                            }
                            pDevice->Release();
                        }

                        if (bFind)
                        {
                            break;
                        }
                    }
                }
                pDevices->Release();
            }
            pEnum->Release();
        }
    }
    CoUninitialize();
}

此样本只能更改主音量的输出.我不知道它是否可以满足您的要求?如果您需要更改其他应用程序,则必须探索一段时间.

This sample can only change the output of Master volume. I don't know whether it can meet your requirements? If you need to change other apps, you have to explore for a while.

这篇关于如何以编程方式为应用程序设置默认的输入和输出音频设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆