如何解决PyAudio 错误:[Errno -9986] 无法获取流信息
我从 PyAudio 文档中获取了线路示例的修改版本:
def mic_to_vac_pipe(self):
microphone_input_stream = self.pyaudio.open(
format=self.pyaudio.get_format_from_width(2),channels=1,rate=self.RATE,input=True,output=True,input_device_index=1,frames_per_buffer=self.CHUNK_SIZE
)
cable_output_stream = self.pyaudio.open(
format=self.pyaudio.get_format_from_width(2),output_device_index=5,# change to 8
frames_per_buffer=self.CHUNK_SIZE
)
# This will last for 217483647 seconds,or 68.09 years
for i in range(0,int(self.RATE / self.CHUNK_SIZE * 2147483647)):
data = microphone_input_stream.read(self.CHUNK_SIZE)
cable_output_stream.write(data,self.CHUNK_SIZE)
microphone_input_stream.stop_stream()
microphone_input_stream.close()
cable_output_stream.stop_stream()
cable_output_stream.close()
这曾经有效,直到我把它移到一个班级,现在我得到这个错误:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\threading.py",line 954,in _bootstrap_inner
self.run()
File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\threading.py",line 892,in run
self._target(*self._args,**self._kwargs)
File "d:\documents\coding\python\kyan\kyan\kyan.py",line 49,in mic_to_vac_pipe
microphone_input_stream = self.pyaudio.open(
File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\site-packages\pyaudio.py",line 750,in open
stream = Stream(self,*args,**kwargs)
File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\site-packages\pyaudio.py",line 441,in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9986] Could not get stream information
我在使用 self.pyaudio.get_host_api_info_by_index(0)
(OSError: [Errno -9978] Invalid host api info
) 时也遇到类似的错误。这些错误可能是相关的吗?
我怀疑它内部可能与 PortAudio 有关,因为绕过 get_host_api_info_by_index(0)
调用(它用于获取音频设备的数量,因此我可以通过为设备计数设置硬编码数字来绕过)告诉我PortAudio 未初始化,即使 PyAudio 已初始化,同时也会初始化 PortAudio。
我该如何解决这个问题?
解决方法
我想通了!
我的问题是类的 pyaudio.terminate()
函数中的 __init__
。当我将它添加到一个类时它停止工作的唯一原因是因为我还将它添加到了一个单独的线程,这涉及调用 thread.join()
,它阻塞了 terminate()
。将此 terminate()
函数移动到正确的位置可以解决我的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。