【问题标题】:PyAudio error: [Errno -9986] Could not get stream informationPyAudio 错误:[Errno -9986] 无法获取流信息
【发布时间】:2021-05-14 00:48:11
【问题描述】:

我从 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()

这曾经可以工作,直到我将它移到一个班级,现在我收到了这个错误:

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。 我该如何解决这个问题?

【问题讨论】:

  • 您的麦克风同时拥有input=Trueoutput=True。很确定它是其中之一。
  • @TimRoberts 我认为两者都有可能,因为您可以使用内置麦克风的耳机。但是无论如何,删除它后,它仍然会给出相同的错误。
  • 错误 -9986 是 paInternalError。不太有用。
  • 我什至尝试过使用调试器,但它并没有指出任何有用的地方

标签: python pyaudio


【解决方案1】:

我想通了! 我的问题是类的__init__ 函数中的pyaudio.terminate()。当我将它添加到一个类时它停止工作的唯一原因是因为我还将它添加到了一个单独的线程中,这涉及到调用thread.join(),它阻塞了terminate()。将此 terminate() 函数移动到正确的位置可以解决我的问题。

【讨论】:

    猜你喜欢
    • 2021-04-02
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多