我正在开发一个播放音乐文件(由用户选择)的GUI应用程序,同时播放这些音乐文件背后的背景音,同时保持响应式GUI主窗口。我试图使用多个线程迭代一个for循环来调用列表中的每个文件并将其传递给pygame,以便用户可以播放它。我已经用pyQt4研究了多线程并尝试了各种不同的方法,我想这里有人可以帮助我。
def playsession(self):
""" Session Player """
if self.sessioncreated is True:
self.statusBar.showMessage("Now Playing...")
for x, i in enumerate(self.finalmusiclist): # A list of all of the files the user has selected to play
mainThread = Thread()
backThread = Thread()
main = SessionPlayer(i)
back = SessionPlayer(self.backgroundsoundslist[x]) # Passing In the Background sounds for iterated loop
main.moveToThread(mainThread)
back.moveToThread(backThread)
mainThread.started.connect(main.play)
mainThread.finished.connect(mainThread.quit)
backThread.started.connect(back.play)
ambThread.finished.connect(backThread.quit)
mainThread.start()
backThread.start()
class Thread(QtCore.QThread):
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
def start(self):
QtCore.QThread.start(self)
def run(self):
QtCore.QThread.run(self)
def __del__(self):
self.wait()
class SessionPlayer(QtCore.QObject):
def __init__(self, playfile):
QtCore.QObject.__init__(self)
self.playfile = playfile
@QtCore.pyqtSlot()
def play(self):
"""Method To Play Music Files With Background Simultaneously"""
pygame.init()
pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
nextsnd = pygame.mixer.Sound(self.playfile)
nextsnd.play()
while pygame.mixer.get_busy():
pass
self.finished.emit()
def __del__(self):
self.wait()
任何和所有帮助将不胜感激!