当前位置: 首页 > news >正文

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。

解决办法:

需要在电脑声音设置中,录制中的立体声混音开启。

打开后,代码正常运行。


http://www.mrgr.cn/news/52089.html

相关文章:

  • 软件测试:快速了解其分类、方法、黑盒测试、白盒测试与灰盒测试
  • 【19楼-注册安全分析报告】
  • SpringBoot接收LocalDateTime参数
  • 白平衡之基于边缘检测的白平衡算法
  • ThreadLocal——简单使用
  • 箭头函数语法及书写规则。
  • AI产品经理:你更适合哪一种?
  • 泛微OA移动引擎中调用手机扫码后跳转另外页面(资产盘点)
  • 是时候扔掉你的密码了
  • 杭州威雅学校:中国橄榄球国家队到访助力杭州威雅校队新赛季之旅
  • 开发一个微信小程序要多少钱?
  • 双十一有哪些必买的好物清单?精选五款双11值得买的好物推荐
  • Quarto ppt模板制作与Rstudio git连接
  • JavaWeb环境下的Spring Boot在线考试系统开发
  • 尚硅谷redis 第97节 redisTmplate下答疑
  • springboot图片上传到本地
  • art虚拟机中的gcroot
  • 证明一个特定形式的函数在其三个正根中,两个较小根处的导数之和小于零
  • 北京十大取保候审律师事务所有哪些
  • Linux 命令 chown 和 chmod 的区别