这是我的异步音频播放器代码。问题是在p.open(...)
之后它会立即调用回调函数。 start_stream
和stop_stream
函数有什么作用?
class AsyncAudioPlayer(object):
def __init__(self, sampling_frequency, sample_width=2):
self.p = PyAudio()
self.buffer = MyBuffer()
self.stream = self.p.open(format=self.p.get_format_from_width(width=sample_width), rate=sampling_frequency,
output=True, channels=1, stream_callback=self.on_ready_to_play)
self.started = False
def close(self):
self.stream.stop_stream()
self.stream.close()
def start(self):
self.started = True
self.stream.start_stream()
def schedule_to_play(self, frame):
self.buffer.put_frames(frame)
if not self.started:
print("Not started. Starting now")
# self.start()
def on_ready_to_play(self, in_data, frame_count, time_info, status_flags):
print("frame count is {}, time_info {}, status flags {}".format(frame_count, time_info, status_flags))
return self.buffer.get_frames(frame_count), paContinue
答案 0 :(得分:1)
来自pyaudio.Stream文档:
开始 - 立即开始播放流。默认为True
。通常,没有理由将其设置为False
。
因此,您只需稍加修改pyaudio.PyAudio.open
来电:
self.stream = self.p.open(format=self.p.get_format_from_width(width=sample_width),
rate=sampling_frequency,output=True, channels=1,
stream_callback=self.on_ready_to_play, start=False)