I am using dotNet c# . I need to identify default video and audio device I do know that I can enumerate audio device but how to determine default one ? ManagementObjectSearc
Thanks to the hard work by @Exergist, and some reading of the code, I acheived the same result in Python so that I can use pyaudio properly. Also, I had to use Level:0 not Role:0. Here is my final result
import winreg as wr
import platform
def is_os_64bit():
return platform.machine().endswith('64')
def get_active_device():
read_access = wr.KEY_READ | wr.KEY_WOW64_64KEY if is_os_64bit() else wr.KEY_READ
audio_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render'
child_key = wr.OpenKeyEx(wr.HKEY_LOCAL_MACHINE, audio_path, 0, read_access)
devices = wr.QueryInfoKey(child_key)[0]
active_last_used = -1
active_device_name = None
for i in range(devices):
device_key_path = f'{audio_path}\\{wr.EnumKey(child_key, i)}'
device_key = wr.OpenKeyEx(wr.HKEY_LOCAL_MACHINE, device_key_path, 0, read_access)
if wr.QueryValueEx(device_key, 'DeviceState')[0] == 1:
properties_path = f'{device_key_path}\\Properties'
properties = wr.OpenKeyEx(wr.HKEY_LOCAL_MACHINE, properties_path, 0, read_access)
device_name = wr.QueryValueEx(properties, '{b3f8fa53-0004-438e-9003-51a46e139bfc},6')[0]
device_type = wr.QueryValueEx(properties, '{a45c254e-df1c-4efd-8020-67d146a850e0},2')[0]
pa_name = f'{device_type} ({device_name})' # name shown in PyAudio
last_used = wr.QueryValueEx(device_key, 'Level:0')[0]
if last_used > active_last_used: # the bigger the number, the more recent it was used
active_last_used = last_used
active_device_name = pa_name
return active_device_name