我在编写一个程序的时候想往程序里添加背景音乐,于是我用了playsound来实现。
def sound():#用多线程播放背景音
while True:
playsound('file/menu.mp3')
t1 = threading.Thread(name='t1',target= sound)
t1.start()
但是它没法停止播放,怎么才能让它停止播放啊
你不是写了while True吗
设置有一个bool变量,is_play=True,不想播放的时候把,is_play=False
while is_play:
import threading
from playsound import playsound
stop_flag = True
def sound():
global stop_flag
while stop_flag:
playsound('file/menu.mp3')
t1 = threading.Thread(name='t1', target=sound)
t1.start()
# 在需要停止播放的地方设置stop_flag = False
stop_flag = False