问题描述
我想在播放声音时更新 tkinter 中的画布图形,以可视化正在播放的声音部分。为此,我使用了 self.figure.canvas.draw() 和随后的 self.figure.canvas.flush_events()。但是,这还不够快,导致声音断断续续。
有没有人想更新更快的图形,这样声音就不会断断续续?也许只选择线条而不是整个图形。
我使用的函数如下所述。我用一个按钮来播放和暂停信号。当播放按钮被点击时,while 循环开始,直到它再次被点击。
def play(self,offset_seconds=0.,duration_seconds=120.,loop=False):
if duration_seconds > 0.:
total_chunks = int((duration_seconds * self.sample_frequency_Hz) / self.chunk)
while self.button_play["text"] == 'Pause sound':
self.index = 0
self.wf.rewind()
# skip first x seconds for debugging purposes
self.wf.readframes(int(self.sample_frequency_Hz*offset_seconds))
# Read data in chunks
data = [0,0]
# Play the sound by writing the audio data to the stream
while (data != '') and (self.button_play["text"] == 'Pause sound'):
self.movingline_audio[0].set_xdata([offset_seconds + self.index * self.chunk / self.sample_frequency_Hz,offset_seconds + self.index * self.chunk / self.sample_frequency_Hz])
self.stream.write(self.wf.readframes(self.chunk))
self.index = self.index + 1
self.figure_4.canvas.draw()
self.figure_4.canvas.flush_events()
if self.index > total_chunks:
print('einde')
break
if loop == False:
break
self.button_play["text"] = 'Play sound'
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)