当前位置: 代码迷 >> 综合 >> python-playsound-线程强制结束
  详细解决方案

python-playsound-线程强制结束

热度:45   发布时间:2024-01-24 18:32:32.0

python-playsound-线程强制结束

目前还有问题。文件有中文名也不行,目前只实现播放,一首播放完了自动切换,终止播放,因为没法实现更多功能,可以看我的另一篇文,基本都实现了

import threading
import time
import playsound
import ctypes
import osdef print_time2():playsound.playsound("one republic - say all i need - gossip girl.mp3")if __name__ == "__main__":# 此方法可行aa = threading.Thread(target=print_time2)tname = aa.getName()aa.start()print(aa.ident)for thread in threading.enumerate():if thread.getName() != 'MainThread':print("bb:"+thread.getName())if thread.getName() == tname:print("kill"+str(thread.ident))time.sleep(3)try:tid = ctypes.c_long(thread.ident)# 正常线程都可以在下一步被kill了,但是os文件占用流的可能不会res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(SystemExit))print(res)while res == 1:print("try to exit")# exit() #失败# quit(0) #失败os._exit(0) # 成功print("exit")except Exception as ex:print(ex)print("endkill")