简体   繁体   English

如何以编程方式设置应用程序的默认输入和输出音频设备?

[英]How can I programmatically set the default input and output audio device for an application?

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.如果我在 Windows 10 (1803) 计算机上转到“设置”,则可以访问一个页面(“应用程序音量和设备首选项”),该页面可让我为正在运行的应用程序设置默认输入和输出设备。

应用程序音量和设备首选项页面的屏幕截图

How can I set these options programmatically?如何以编程方式设置这些选项?

Related:有关的:

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;
}

Here you need to use PolicyConfig.h and SetDefaultAudioPlaybackDevice to add .h files and interfaces.这里需要使用 PolicyConfig.h 和 SetDefaultAudioPlaybackDevice 添加 .h 文件和接口。 Refer to this project参考这个项目

1.Add the header file PolicyConfig.h 1.添加头文件PolicyConfig.h

2.Add the head file and interface. 2.添加头文件和接口。

#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.Use the above interface to write a function to set the default output device. 3.使用上面的接口写一个函数来设置默认输出设备。

It's MFC Project.这是 MFC 项目。 Maybe you need to change.也许你需要改变。

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()我使用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.如果需要换其他应用,就得自己摸索了。

So I have been using SoundVolumeView for a while that let me mute and unmute my mic for meeting with a command line and I have discovered recently (because of OBS and monitoring audio) that it can also change device for an app or global default device所以我一直在使用 SoundVolumeView 一段时间,这让我可以将我的麦克风静音和取消静音以与命令行会面,我最近发现(由于 OBS 和监控音频)它还可以更改应用程序或全局默认设备的设备

And using /SetDefault and /SetAppDefault as shown in the doc example to the bottom of the page并使用 /SetDefault 和 /SetAppDefault 如页面底部的文档示例中所示

I have put that in a batch script and bind a macro to my keyboard and it's doing a good job so far :)我已经把它放在一个批处理脚本中并将一个宏绑定到我的键盘上,到目前为止它做得很好:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在默认音频设备上设置权限,以便可以以编程方式更改它? - How to set permissions on the default audio device so that it can be changed programmatically? 如何在Windows 7中更改音频输出设备的默认共享模式采样率? - How can I change audio output device default share mode sample rate in Windows 7? 如何通过 Node JS(或命令行)更改系统音频输出/输入设备 - How can I change my system audio output / input device through Node JS (or command line) 如何以编程方式更改默认音频输入设备 - How to change default audio input device programatically 如何在Visual Basic .NET中找到默认音频设备? - How can I find the default audio device in Visual Basic .NET? 如何检测默认音频 output 设备更改 Windows - How to detect default audio output device change in Windows 以编程方式设置在Windows中“收听”的音频设备 - Programmatically set the audio device being “listened” to in Windows 如何从Java应用程序制作音频输入设备 - How to make audio input device from java application 如何控制 windows 中的(虚拟)音频设备的输入 stream? - How can I take control of the input stream of a (virtual) audio device in windows? 如何将应用程序设置为以编程方式打开某种类型文件的默认程序? - How to set an application as the default program of opening a certain type of file programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM