Search code examples
pythonwindows-7tkinterwin32com

Simple Music Player Wont Play


I'm trying to write a simple music player. For some reason after I select a mp3 file and hit the play button, no sound occurs. No error, nothing. It seems to skip right over the command. Anyone know why it may be doing this? If there is a better way to play music with python, what is it?

from win32com.client import Dispatch

import Tkinter
import tkFileDialog

class PlayerWin (Tkinter.Tk) :
    def __init__ (self) :
        self.Dir = None


        Tkinter.Tk.__init__(self)


        Tkinter.Button(self, text='File', command=self.select_file ).pack()

        Tkinter.Button(self, text=' ► ', command=self.play ,font=('Arial', 10 ,'bold')).pack()


    def select_file (self) :
        _dir = tkFileDialog.askopenfilename()
        self.Dir = _dir

    def play (self) :

        mp = Dispatch('WMPlayer.OCX')

        if self.Dir != None :
            print self.Dir
            song = mp.newMedia(self.Dir)
            mp.currentPlaylist.appendItem(song)
            mp.controls.play()



if __name__ == '__main__' :
    PlayerWin().mainloop()

Solution

  • This probably has something to do with threading.

    In any case, there are other GUI toolkits for Python, such as WxPython, and there are mp3 player apps written in Python that you could study.