[英]terminating a thread that plays a ringtone in a voip app
我需要使用python和PyQt5编写的voip应用程序的线程。 该线程执行正在播放的铃声。 但是我不知道如何停止该线程的播放,我需要一种类似“在Process中terminate
的方法来干净利落地杀死线程,但是我找不到。 我不得不提到我不能使用Process和multiProcessing(存在一个问题,我找不到一种以最小的方式显示它的方法)。
这是我的问题的最小化代码:
import playsound
# from multiprocessing import Process
from playsound import playsound
from threading import Thread
class Test:
def __init__(self):
self.ringTone = True
def ringTonePlay(self):
while self.ringTone:
playsound("ring_tone.wav")
def testFunction(self):
self.p1 = Thread(target = self.ringTonePlay)
self.p1.start()
def stopFunction(self):
self.ringTone = False
test = Test()
test.testFunction()
test.stopFunction()
当我调用stopFunction
函数时,它仍然保持振铃。 您建议终止播放线程的方式是什么?
您可以使用threading.Event 。 您可以根据需要阻止和取消阻止线程。
编辑:如果playsound
在不同的线程中播放给定的声音,则可能需要查看有关如何停止/阻止它的文档。
例:
import playsound
import threading
from playsound import playsound
class Ringtone(threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.event = threading.Event()
def run(self):
while True:
self.event.wait()
playsound("ring_tone.wav")
ringtone = Ringtone(1, "Ringtone")
ringtone.start()
# To block the thread
ringtone.event.clear()
# To unblock the thread
ringtone.event.set()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.