有没有办法做到这一点winsound
;这是一个简单而简单的模块。
然而,有一个非常简单的方法来间接地做到这一点:创建一个后台线程同步播放声音,然后设置一个标志。甚至只是使用线程本身作为标志。例如:
import threading
import winsound
t = threading.Thread(target=winsound.PlaySound, args=[sound, flags])
while True:
do_some_stuff()
t.join(0)
if not t.is_alive():
# the sound has stopped, do something
在另一方面,如果你想要做的就是尽快发挥另一种声音,因为每一个末端,只是把他们都在一个队列:
import queue
import threading
import winsound
def player(q):
while True:
sound, flags = q.get()
winsound.PlaySound(sound, flags)
q = queue.Queue()
t = threading.Thread(target=player, args=[q])
t.daemon = True
q.push(a_sound, some_flags)
q.push(another_sound, some_flags)
do_stuff_for_a_long_time()