[英]PyAudio error: [Errno -9986] Could not get stream information
I've taken a modified version of the wire example from the PyAudio docs:我从 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),
channels=1,
rate=self.RATE,
output=True,
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()
This used to work, until I moved it to a class, and now I get this error:这曾经有效,直到我将其移至 class,现在我收到此错误:
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
I also get a similar error when using self.pyaudio.get_host_api_info_by_index(0)
( OSError: [Errno -9978] Invalid host api info
).使用
self.pyaudio.get_host_api_info_by_index(0)
时,我也会遇到类似的错误( OSError: [Errno -9978] Invalid host api info
)。 Is it possible these errors are related?这些错误是否可能相关?
I suspect it might be related to PortAudio internally, since bypassing the get_host_api_info_by_index(0)
call (it's used to get the number of audio devices, so I can bypass by setting a hardcoded number for a device count) tells me that PortAudio is not initalized, even though PyAudio is initalized, which also initalizes PortAudio at the same time.我怀疑它可能与内部的 PortAudio 有关,因为绕过
get_host_api_info_by_index(0)
调用(它用于获取音频设备的数量,所以我可以通过为设备计数设置硬编码数字来绕过)告诉我 PortAudio 没有初始化,即使 PyAudio 被初始化,它也会同时初始化 PortAudio。 How can I fix this?我怎样才能解决这个问题?
I figured it out!我想到了! My problem was that
pyaudio.terminate()
in the class's __init__
function.我的问题是类的
__init__
function 中的pyaudio.terminate()
。 The only reason it stopped working when I added it to a class is because I also added it to a separate thread, which involves calling thread.join()
, which is blocking terminate()
.当我将它添加到 class 时它停止工作的唯一原因是因为我还将它添加到了一个单独的线程中,这涉及调用
thread.join()
,它阻塞了terminate()
。 Moving this terminate()
function to the proper spot fixes my problem.将此
terminate()
function 移动到正确的位置可以解决我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.