import comtypes
from pycaw.pycaw import AudioUtilities, IMMDeviceEnumerator, EDataFlow, DEVICE_STATE
from pycaw.constants import CLSID_MMDeviceEnumerator
devices = []
deviceEnumerator = comtypes.CoCreateInstance(
CLSID_MMDeviceEnumerator,
IMMDeviceEnumerator,
comtypes.CLSCTX_INPROC_SERVER)
if deviceEnumerator is None:
devices = []
raise ValueError("Couldn't find any devices.")
collection = deviceEnumerator.EnumAudioEndpoints(EDataFlow.eCapture.value, DEVICE_STATE.ACTIVE.value)
if collection is None:
devices = []
raise ValueError("Couldn't find any devices.")
count = collection.GetCount()
for i in range(count):
dev = collection.Item(i)
if dev is not None:
if not ": None" in str(AudioUtilities.CreateDevice(dev)):
devices.append(AudioUtilities.CreateDevice(dev))
for device in devices:
print("Found input device.")
print(device.FriendlyName)
print(device.id)
print("\n")
2条答案
按热度按时间unhi4e5o1#
我尝试了很多和一个简单的方法来找到设备ID在Windows中。我想分享我的学习。
阅读此处的Microsoft文档时,我了解到设备ID的格式为{0.0.1.00000000}。{5 f23 ab 69 -6181- 4f 4a-81 a4 - 45414013 aac 8}
我浏览了设备管理器中设备驱动程序的所有属性,发现名为设备示例路径的属性。值将类似于SWD\MMDEVAPI{0.0.1.00000000}.{8 c85 a4 e8 - 3a 66 -4857-8883- 96 d100179 b47}。如果保留初始部分,则值代表设备ID。
{0.0.1.00000000}.{8c85a4e8-3a66-4857-8883-96d100179b47}
qf9go6mv2#
由于您使用的是python,因此可以使用pycaw,它允许您使用windows CoreAudio API。
下面是一个如何列出所有输入设备及其ID的示例,改编自this github issue: