MP3播放模块将在线程Python中运行

2024-04-25 11:30:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写一个程序,将播放MP3的。这个程序也将有一个图形用户界面。为了让两者同时发生,我正在实现线程化。但是,我遇到了一个问题。每当我运行我的程序,一切正常执行,除了没有声音出来。没有错误没什么。好像命令被跳过了。通过消除过程,我相信我已经发现问题是由我在一个线程内运行程序的音乐播放部分引起的(自我控制.play())。在

有没有人熟悉其他模块,可以让我在一个线程内播放音乐?或者有人知道是什么导致了这个问题?在

代码:

import GUIThread as GUIT # GUIThread is just an interface I made for the threading module, just act as if it was threading.  
from win32com.client import Dispatch

class Test :
    def __init__ (self) :
        self.Dir = r'song.mp3'
        self.Thread = GUIT.Thread(self, func=self.play_song, loop=False)
        self.MP = Dispatch('WMPlayer.OCX')
        song = self.MP.newMedia(self.Dir)
        self.MP.currentPlaylist.appendItem(song)

    def start (self) :
        # Starts the thread. 
        # Equivalent to :
        # t = threading.Thread(target=self.play_song)
        # t.start()
        self.Thread.start()

    def play_song (self) :
        # This whole function is done within the thread.

        if self.Dir != None :
            print self.Dir

            self.MP.controls.play()


    def protocol (self, val0, val1) :
        # Ignore this it's unrelated
        pass

T = Test()
T.start()

顺便说一句,这是在Windows7中

编辑: 另外,我将在GUI工具箱中使用Tkinter。在


Tags: theimportself程序playsong音乐def