pyaudio出现Invalid number of channels的解决方法
最近准备做个录制电脑本身的音频,于是用到了pyaudio。代码如下。
import pyaudiodef get_audio_devices():p = pyaudio.PyAudio()device_info = p.get_host_api_info_by_index(0)device_count = device_info.get('deviceCount')devices = []for i in range(device_count):device = p.get_device_info_by_host_api_device_index(0, i)devices.append(device)p.terminate()return devicesdef open_audio_stream(device_index):p = pyaudio.PyAudio()stream = p.open(format=pyaudio.paInt16, channels=2, rate=48000, input=True, input_device_index=device_index,frames_per_buffer=1024)return p, streamdef read_audio_data(stream, chunk_size=1024):data = stream.read(chunk_size)return datadef close_audio_stream(p, stream):stream.stop_stream()stream.close()p.terminate()# 获取音频设备
devices = get_audio_devices()
print("可用的音频设备:")
for i, devices in enumerate(devices):print(devices)
open_audio_stream(1)
结果报错:OSError: [Errno -9998] Invalid number of channels。
通道错误,打印音频设备时,发现maxInputChannels这个值为0。
解决办法:
需要在电脑声音设置中,录制中的立体声混音开启。
打开后,代码正常运行。