python - Pygame 和线程

标签 python multithreading pygame

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/

相关文章:

Python Itertools Combinatinos 检索空列表

java - 如何每秒移动 jlabel?

multithreading - 追踪内核中神秘的高优先级线程挂起

Pygame 版本与 python 3.6

python - setup.py 包和 unicode_literals

python - 在Python中使用Xpath按属性选择祖先节点

multithreading - OCaml 中的线程延迟和键盘事件

python - 嵌套的 if 语句、干净的代码和 Pythonic,带有 Controller /键盘输入

python - 如何限制pygame中的 Sprite 列表

python - Python套接字:Converting bytes object