wxPython MediaCtrl Tutorial

This is just a short post to explain something I had a hard time understanding: How to get the media player working for Windows 7, (64bit, if that matters here, I’m not actually sure).

I never fully understood what it took to start playing a song, since I assumed that I needed to just load the song into the media player ctrl, then play it, but there was a subtle thing I needed to do as well, and that was to catch the event EVT_MEDIA_LOADED.

before I could sucessfully play the mp3, as well as change the backend of the mediaCtrl to WMP10, instead of leaving it as default. The part that really through me for a loop was that all the docs I had read on this, just loaded and then played the song right away. Seems like there’s a bug somewhere that doesn’t catch that event on Windows 7.

Anyways, here’s a simple script, after the jump, that lets you play one single song, hardcoded into the file, if you want to change the song to play, you’ll have to change the filepath in the code.

#import wx python
import wx, wx.media

#----------------------------------------------------------------------
def button_pressed(e):
    """this function gets called when btn gets clicked,
    which loads the mp3 file into the player"""
    print 'button pressed'
    frm.player.Load(mp3_path)

#----------------------------------------------------------------------
def song_is_loaded(e,):
    """this function gets called when the EVT_MEDIA_LOADED is created
    , then  it plays the song that gets loaded."""
    frm.player.Play()
    print 'evt loaded'

#sets the path to the mp3 file
mp3_path = r'YOUR_MP3_FILE_HERE.mp3'

#creates a wxPython App with the 'print' output sent to the terminal, rather
# than the wx log
app = wx.App(redirect=False)

#create  Frame without any parent
frm = wx.Frame(None, id=wx.ID_ANY)
#creates the MediaCtrl, invisible to us, with the frame as a parent, and
# the WMP10 backend set
frm.player = wx.media.MediaCtrl(parent=frm, szBackend=wx.media.MEDIABACKEND_WMP10)

#Binds the EVT_MEDIA_LOADED to the function song_is_loaded written above,
# so that when the song gets loaded to memory, it actually gets played.
frm.Bind(wx.media.EVT_MEDIA_LOADED, song_is_loaded)

#a wx Sizer so that the two widjets gets places
main_sizer = wx.BoxSizer()

#the play button, to start the load
btn = wx.Button(frm, label='Play')
#binding the load mp3 function above to the clicking of this button
btn.Bind(wx.EVT_BUTTON, button_pressed)

#add the player and the button the sizer, to be placed on the screen
main_sizer.Add(frm.player)
main_sizer.Add(btn)

#declare the main_sizer as the top sizer for the entire frame
frm.SetSizer(main_sizer)
#show the frame to the user
frm.Show()

#start the mainloop, required for any wxPython app. It kicks off the logic.
print 'running'
app.MainLoop()

Some fun facts about this code: It’ll actually play movies too, it just won’t have anyway to control it. It’s also important to remember that you need to have the mp3 file in the same folder as the script OR have the full path set. Please let me know if you had any trouble with this tutorial, and I’ll do my best to you out.

Enjoy!

Tagged , , , , , . Bookmark the permalink.

2 Responses to wxPython MediaCtrl Tutorial

  1. Alex says:

    Thankyou so much mate this was really bugging me for such a long time !! :D

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>