这个问题是在这里提出并回答的:Stop the audio from playing in pydub
然而,只有一个答案不适用于我。我在Ubuntu18.04上使用pydub,Python3.6。
我尝试了以下代码的一些变体,包括以下代码:
def ring():
sound_alarm = True
while True:
try:
print(f'sound_alarm is {sound_alarm}')
if sound_alarm == False:
return
play(sound)
except KeyboardInterrupt:
sound_alarm = False
print('done playing sound.')
break我得到以下信息:
sound_alarm is True
^Csound_alarm is True
^Csound_alarm is True
sound_alarm is True我让它循环一次,试着杀死它两次,很明显,它一直在循环。
我试过用播放声音代替,但这种行为绝对不是我喜欢的。它不会立即停止播放。它确实停止了播放,但给出了一个非常难看的堆栈跟踪(见下文)。不过,除非我现在就杀了它,否则这似乎不是一个好的解决办法。
(python3.6:16669): GStreamer-CRITICAL **: 11:18:46.456:
Trying to dispose element playbin, but it is in PLAYING instead of the NULL state.
You need to explicitly set elements to the NULL state before
dropping the final reference, to allow them to clean up.
This problem may also be caused by a refcounting bug in the
application or some element.发布于 2019-10-25 13:33:17
我建议使用simpleaudio进行回放(如果安装了,pydub将默认使用它)。它应该立即响应键盘中断,因为它在一个单独的线程中进行回放。
https://stackoverflow.com/questions/58290622
复制相似问题