Mitch
Mitch

Reputation: 22301

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.

Screenshot of App Volume and Device Preferences page

How can I set these options programmatically?

Related:

Upvotes: 10

Views: 12276

Answers (3)

Jaryt
Jaryt

Reputation: 21

@slhad's answer saved me here! I've done a lot of digging on this topic, but wasn't able to achieve it in C#, or just basic powershell. However, using SoundVolumeView I was able to achieve it.

Here's my Powershell script that you can use to get started. My use case is switching Spotify between my headphones and speakers.

# paths
$root = "F:\Programs\Sound Volume View"
$soundVolumeViewPath = "$root\SoundVolumeView.exe"
$deviceFilePath = "$root\current_device"

# audio device root paths
$deviceID1 = "Scarlett Solo USB"
$deviceID2 = "2- High Definition Audio Device"

$currentDeviceID = Get-Content $deviceFilePath

if ($currentDeviceID -eq $deviceID1) {
    $setDeviceID = $deviceID2
} else {
    $setDeviceID = $deviceID1
}

$setDeviceID | Out-File $deviceFilePath

& $soundVolumeViewPath /SetAppDefault "$setDeviceID\Device\Speakers\Render" all "Spotify.exe"

Everything is very hard-coded for my use-case, so some customization will be needed for any future wanderers. My implementation reads from a file, gets the "current" device, and swaps Spotify's default audio device to the other. Works great.

By using auto hot key I was able to set it up to a keybind (Win+J):

#Requires AutoHotkey v2.0

#j::Run 'powershell.exe -windowstyle hidden -ExecutionPolicy Bypass -File "F:\Programs\Sound Volume View\spotify.ps1"' ,, "Hide"

I recommend compiling that to an exe (this is done through AHK), and then adding it to your Startup directory (%AppData%\Microsoft\Windows\Start Menu\Programs\Startup) for ease of use.

Upvotes: 0

slhad
slhad

Reputation: 41

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

And using /SetDefault and /SetAppDefault as shown in the doc example to the bottom of the page

I have put that in a batch script and bind a macro to my keyboard and it's doing a good job so far :)

Upvotes: 4

Jeffreys
Jeffreys

Reputation: 451

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. Refer to this project

1.Add the header file PolicyConfig.h

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.Use the above interface to write a function to set the default output device.

It's MFC Project. 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()

//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.

Upvotes: 8

Related Questions