gpt4 book ai didi

python - Pygame 和线程

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:09 24 4
gpt4 key购买 nike

def on_press(self):
file_name = self.filename
if(self.flag1<3):
var="thread1"+str(self.flag1)
var=myThread(1,file_name)
self.flag1=self.flag1+1
print "Thread count is "+str(threading.active_count())
var.start()

这里flag1最初设置为1。每次我按下前端的按钮时,都会调用函数on_press()

我的myThread类():

 class myThread (threading.Thread):
def __init__(self,counter,file_name):
threading.Thread.__init__(self)
self.counter = counter
self.file_name=file_name
def run(self):
print "Starting "
channel=pygame.mixer.music.load(self.file_name)
channel=pygame.mixer.music.play()
print "Exiting "

现在我的问题是每次我调用 on_press() 函数时都会创建新线程,并且它会覆盖正在播放的任何音频文件。我希望所有线程同时播放音频文件。新线程不应覆盖其他已创建线程播放的音乐。我该怎么做?

最佳答案

如果您想一次播放多个声音,请使用 Sound类。

来自 music module 的文档(强调我的):

The difference between the music playback and regular Sound playback is that the music is streamed, and never actually loaded all at once. The mixer system only supports a single music stream at once.

关于python - Pygame 和线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23166163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com