Search code examples
pythonmultithreadinguser-interfacemp3

MP3 Playing modules that will run within a thread Python


I'm trying to write a program that will play MP3's. This program will also have a GUI. In order to allow both to happen simultaneously I'm implementing threading. However, I've run into a problem. Whenever I run my program everything executes normally, except no sound comes out. No errors nothing. It's as if the command was skipped. Through process of elimination I believe I've found the problem is caused when I run the music playing portion of the program from within a thread (self.MP.controls.play()).

Is anyone familiar with other modules that will allow me to play music within a thread? Or is anyone aware of what may be causing the problem?

Code:

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()

This is in Windows 7, BTW.

Edit: Also, I'm going to be using Tkinter for the GUI toolkit.


Solution

  • You could try pymedia or pyglet. I know pygame has mp3 support as well.