我想在回调模式下使用 pyAudio 并使用 stream = pyaudio.PyAudio().open(...) 准备流。我希望在使用 stream.start_stream() 启动流后调用我的 callback 方法。但是,我的回调函数在创建流对象后立即被调用(参见演示代码)。
我使用的 pyAudio 是错误的还是这是一个错误?
import time
import pyaudio
def callback(in_data, frame_count, time_info, status):
print("CALLBACK")
return (in_data, pyaudio.paContinue)
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(2), channels=1, rate=44100,
input=True, output=False, stream_callback=callback,
frames_per_buffer=5000)
# XXX: Note that I do not call stream.start_stream() here
time.sleep(5)
stream.close()
我正在使用 python 3.6.5 和 pyaudio 0.2.11 以及 portaudio 19.6.0
请您参考如下方法:
pyaudio.Stream 的初始化程序(open 初始化)默认启动流。如果您不希望它以 PyAudio.open 开头,您可以传递 start=False。
摘自 the documentation (强调我的):
__init__(PA_manager, rate, channels, ... , start=True, ...)Parameters:
...
start– Start the stream running immediately. Defaults toTrue. In general, there is no reason to set this to False.
