Open askruga opened 3 years ago
@askruga i need this too
These needs IPolicyConfig
. In AutoHotKey it could be implemented as follows.
SetDefaultEndpoint(DeviceID, Role := 3)
{
; Undocumented COM-interface IPolicyConfig.
IPolicyConfig := ComObject("{870AF99C-171D-4F9E-AF0D-E63Df40c2BC9}", "{F8679F50-850A-41CF-9C72-430F290290C8}")
if (Role & 0x1)
ComCall(13, IPolicyConfig, "Str", DeviceID, "Int", 0) ; Default Device
if (Role & 0x2)
ComCall(13, IPolicyConfig, "Str", DeviceID, "Int", 2) ; Default Communication Device
}
Stack overflow https://stackoverflow.com/questions/13054371/programmatically-switch-audio-devices-on-windows-7
Calling COM https://www.oreilly.com/library/view/python-programming-on/1565926218/ch05s02.html
Tried to do it but I can't find the part that allows me to do the two-parameter version ComObject that allows passing in a CLSID and IID in w32com.client
def switchOutput(deviceId, role):
policy_config = comtypes.CoCreateInstance(
pc.CLSID_PolicyConfigClient,
pc.IPolicyConfig,
comtypes.CLSCTX_ALL
)
policy_config.SetDefaultEndpoint(deviceId, role)
policy_config.Release()
You can change audio devices using this function. You can get list of audio devices using EnumAudioEndpoints
. and role arg is from EDataFlow
has 4 options, Output/Input/Default/Communications
how can I listen event of Switching current default audio device?
def switchOutput(deviceId, role): policy_config = comtypes.CoCreateInstance( pc.CLSID_PolicyConfigClient, pc.IPolicyConfig, comtypes.CLSCTX_ALL ) policy_config.SetDefaultEndpoint(deviceId, role) policy_config.Release()
You can change audio devices using this function. You can get list of audio devices using
EnumAudioEndpoints
. and role arg is fromEDataFlow
has 4 options, Output/Input/Default/Communications
Hey @KillerBOSS2019
What modules do I need to import for this to work? What is pc
?
Thanks!
def switchOutput(deviceId, role): policy_config = comtypes.CoCreateInstance( pc.CLSID_PolicyConfigClient, pc.IPolicyConfig, comtypes.CLSCTX_ALL ) policy_config.SetDefaultEndpoint(deviceId, role) policy_config.Release()
You can change audio devices using this function. You can get list of audio devices using
EnumAudioEndpoints
. and role arg is fromEDataFlow
has 4 options, Output/Input/Default/CommunicationsHey @KillerBOSS2019 What modules do I need to import for this to work? What is
pc
? Thanks!
Did you figure out what the pc
import is supposed to be? This is so close to the exactly solution I need. I'm guessing it's supposed to model the same thing as https://github.com/frgnca/AudioDeviceCmdlets/blob/master/SOURCE/IPolicyConfig.cs, but I can't find those interface types predefined anywhere
def switchOutput(deviceId, role): policy_config = comtypes.CoCreateInstance( pc.CLSID_PolicyConfigClient, pc.IPolicyConfig, comtypes.CLSCTX_ALL ) policy_config.SetDefaultEndpoint(deviceId, role) policy_config.Release()
You can change audio devices using this function. You can get list of audio devices using
EnumAudioEndpoints
. and role arg is fromEDataFlow
has 4 options, Output/Input/Default/Communications
It would be super helpful if you could throw togother a quick example of how to actually call this method. I can't find interface definitions for IPolicyConfig anywhere
Luckily I have my project open source (: https://github.com/KillerBOSS2019/TouchPortal-Windows-MediaMixer/blob/main/src/audioUtil/policyconfig.py
My project allows me to interface the audio mixer directly, changing individual application output/input and switching global audio devices
Thanks for the link! I figured out the clsid thing on my own but was stuck at the IPolicyConfig. I'll have a look at your project
changing individual application output/input
This is exactly what I need 😄
This is excellent! I had started the process of implementing the IPolicyConfig
interface myself and quickly realized it wasn't worth the trouble and resulted to just using this powershell module instead: https://github.com/KillerBOSS2019/TouchPortal-Windows-MediaMixer/tree/main, which meant it wouldn't be a purely python solution. This worked for my purposes perfectly!
Got it working! Thanks Two questions:
Is there a way to get default audio input device change event from windows for python?
@Dearex 1. I have tried to translate the program into python but no success. The dll is a simplified version of AudioSwitcher program. Basically a minimal required code to get it working. As for the second question. You will need to do your own research. I have that somewhere just Donno where
Would be cool if you can reference my project if you use part of my project. Not required (:
Hi. Is it possible to:
1.Get a list of audio devices 2.Get current default audio device 3.Change the default device by your lib?
Or maybe anyone have idea how do this? Thanks for lib!