简体   繁体   English

Python PyAudio 在麦克风断开连接时更新程序中的音频设备信息

[英]Python PyAudio Updating audio devices info in program when mic disconnected

Win10/ I have a loop where I listen to the background with a microphone. Win10/ 我有一个循环,我可以用麦克风收听背景音乐。 I have a function, which shows me my current system audio devices (I setup the function to show microphones only).我有一个功能,可以显示我当前的系统音频设备(我设置了仅显示麦克风的功能)。

Here it is:这里是:

def get_mics_list():
    mics = []
    p = pyaudio.PyAudio()
    for i in range(p.get_device_count()):
        if p.get_device_info_by_index(i)['name'] == 'Microsoft Sound Mapper - Input':
            pass
        elif p.get_device_info_by_index(i)['name'] == 'Microsoft Sound Mapper - Output':
            break
        else:
            mics.append(p.get_device_info_by_index(i))
    return mics

I chose the constant microphone device index for looping.我选择了用于循环的恒定麦克风设备索引。 Index = 1. The default microphone in the system. Index = 1. 系统中的默认麦克风。 When I disconnect the microphone while looping, I catch the exception and continue looping with second (integrated in laptop) microphone, but I cannot update information about the current available system devices with that function.当我在循环时断开麦克风时,我捕获到异常并继续使用第二个(集成在笔记本电脑中)麦克风循环,但我无法使用该功能更新有关当前可用系统设备的信息。 It still shows me two microphones, as it was before disconnection.它仍然显示两个麦克风,就像断开连接之前一样。

How can I update my system audio devices information in the program after changing the microphone?更换麦克风后如何在程序中更新我的系统音频设备信息?

I found this question and was able to come up with a solution.我发现了这个问题并且能够想出一个解决方案。 Apparently, PyAudio does not allow you to update the list of devices without reinitializing it .显然,PyAudio 不允许您在不重新初始化的情况下更新设备列表。 I don't know if anyone else needs this, but here's my solution:我不知道是否还有其他人需要这个,但这是我的解决方案:

I managed to update the list of devices only by calling the PyAudio again.我设法仅通过再次调用 PyAudio 来更新设备列表。 So, make a def like this:所以,像这样做一个定义:

def init_py_audio():
    # Opened for the first time
    if py_audio is None:
        py_audio = pyaudio.PyAudio()
    
    # Refresh
    else:
        py_audio.terminate()
        py_audio = pyaudio.PyAudio()

And further, before getting a list of devices, call it like this:此外,在获取设备列表之前,可以这样称呼它:

import pyaudio

py_audio = None


def get_mics_list():
    # Better use it inside a class and don't use global
    global py_audio

    # Opened for the first time
    if py_audio is None:
        py_audio = pyaudio.PyAudio()

    # Refresh
    else:
        py_audio.terminate()
        py_audio = pyaudio.PyAudio()

    mics = []
    info = py_audio.get_host_api_info_by_index(0)

    # List all devices
    for i in range(0, info.get('deviceCount')):
        # Check number of input channels
        # (If there is at least 1 input channel, then it is suitable as a microphone)
        if py_audio.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels') > 0:
            mics.append(py_audio.get_device_info_by_host_api_device_index(0, i).get('name'))

    return mics

# Will update the device list every time it is called
print(get_mics_list())

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM