Everything seems to be well, but NOTHING GET PLAYED!!!! :oops:
I have tried and tried but doesn't work. WHAT IS WRONG???? :shock:

#media control
#Copyright  2008 Evstevemd
#Just media Player to play the playee
import wx
import wx.media as media
import os, sys

class MediaPlayer(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size = (600, 400))
        self.panel = wx.Panel(self, -1)
        #self.panel.SetBackgroundColour('grey')
        
        #Main Sizer
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        
        #Media Control
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        self.player = media.MediaCtrl(self.panel, -1, size = (400, 300))
        hbox.Add(self.player, 1, wx.EXPAND)
        #playlist and its Title in same sizer
        playlist_sizer = wx.BoxSizer(wx.VERTICAL)
        self.file_list = []
        #Playlist title
        self.playlist_title = wx.StaticText(self.panel, -1, "Current Play List", style = wx.ALIGN_CENTER)
        playlist_sizer.Add(self.playlist_title , 0, wx.EXPAND |wx.ALL, 5)
        #Play List display 
        self.playlist = wx.ListCtrl(self.panel, -1, size = (250, 300), style = wx.LC_REPORT)
        self.playlist.InsertColumn(col=0, heading = "Song Name", width = 150)
        self.playlist.InsertColumn(col=1, heading = "Directory", width = 100)
        self.playlist.InsertColumn(col=2, heading = "Artist", width = 175) 
        #operate
       

        playlist_sizer.Add(self.playlist , 1, wx.EXPAND |wx.LEFT|wx.RIGHT, 5)
        #Add them to a sizer
        hbox.Add(playlist_sizer, 1, wx.EXPAND)
        main_sizer.Add(hbox,1, wx.EXPAND)
        
        #Load widgets sizers & Bind to events
        hsizer1 = wx.BoxSizer()
        #open
        self.bopen = wx.Button(self.panel, -1, "Open File" )
        hsizer1.Add(self.bopen, 0, wx.ALL, 5)        
        self.Bind(wx.EVT_BUTTON, self.onLoad, id = self.bopen.GetId() )
        #stop
        self.bstop = wx.BitmapButton(self.panel, -1, wx.Bitmap("./icon/media-playback-stop.png"),size = (22, 22) )
        hsizer1.Add(self.bstop, 0, wx.LEFT|wx.TOP|wx.BOTTOM, 5)
        self.Bind(wx.EVT_BUTTON, self.onStop, id = self.bstop.GetId())
        #Play
        self.bplay = wx.BitmapButton(self.panel, -1,wx.Bitmap("./icon/media-playback-start.png"),size = (22, 22) )
        hsizer1.Add(self.bplay, 0,  wx.LEFT|wx.TOP|wx.BOTTOM, 5)
        self.Bind(wx.EVT_BUTTON, self.onPlay, id = self.bplay.GetId())
        #Pause
        self.bpause = wx.BitmapButton(self.panel, -1,wx.Bitmap("./icon/media-playback-pause.png"),size = (22, 22))
        hsizer1.Add(self.bpause, 0, wx.LEFT|wx.TOP|wx.BOTTOM, 5)
        self.Bind(wx.EVT_BUTTON, self.onPause, id = self.bpause.GetId())
        #Slider
        vsizer1 = wx.BoxSizer(wx.VERTICAL)        
        self.slider = wx.Slider(self.panel, -1,size = (150,-1))
        vsizer1.Add(self.slider, 0, wx.ALL, 5)
        self.Bind(wx.EVT_SLIDER, self.onSlide)
        
        #Volume Label
        volume = self.player.GetVolume()
        self.label = wx.StaticText(self.panel, -1, "Volume: %s" %(volume,))
        vsizer1.Add(self.label, 0, wx.LEFT|wx.TOP|wx.BOTTOM, 5)
        
        hsizer1.Add(vsizer1, 0, wx.EXPAND)
        
        #Volume up
        vsizer2 = wx.BoxSizer(wx.VERTICAL)
        self.volup = wx.Button(self.panel, -1, "Vol +", size = (40, 22))
        vsizer2.Add(self.volup, 0, wx.LEFT|wx.TOP|wx.BOTTOM, 5)        
        self.Bind(wx.EVT_BUTTON, self.onVolup, id = self.volup.GetId() )
        
        #Volume down
        self.voldown = wx.Button(self.panel, -1, "Vol -", size = (40, 22))
        vsizer2.Add(self.voldown, 0, wx.ALL, 5)        
        self.Bind(wx.EVT_BUTTON, self.onVoldown, id = self.voldown.GetId() )
        
        hsizer1.Add( vsizer2, 0, wx.EXPAND)
        
        #Add to main sizer
        main_sizer.Add(hsizer1, 0, wx.EXPAND)
        self.panel.SetSizer(main_sizer)
        self.panel.Layout()
   
     #Events
        
        
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer)
        self.timer.Start(100)
        
        #self.panel.SetInitialSize()
        #self.SetInitialSize()
        
        
       
    #Load File
    def onLoad(self, evt):
        msg = wx.FileDialog(self, message = "Open a media file", style = wx.OPEN |wx.FD_CHANGE_DIR |wx.FD_MULTIPLE, wildcard = "Media Files|*.wma;*.mp3;*.avi")
        if msg.ShowModal() == wx.ID_OK:
            paths = msg.GetPaths()
        self.onLoadPlaylist(paths)
            
            
               
            
    #Check format
    def onCheck(self, path):
        self.path = path            
        if not self.player.Load(path):
            wx.MessageBox("Unable to Play this file, it is in the wrong format")
        else:
            print "Loaded! %s" %(self.path,)
            self.player.Play()
            
        
            
    #play it
    def onPlay(self, evt):
        self.onSetPath()
        self.player.Play()
        print self.player.Play()
        print 'now PLAYING'        
        self.slider.SetRange(0, self.player.Length())
        self.player.SetVolume(0.404459081855)
        volume = str(self.player.GetVolume())
        self.label.SetLabel("Vol: %s" %(volume,))
            
    #Stop
    def onStop(self, evt):
        self.player.Stop()
        
    #Pause
    def onPause(self, evt):
        self.player.Pause()
        
    #Timer 
    def onTimer(self, evt):
        offset = self.player.Tell()
        self.slider.SetValue(offset)
        
    #Slider
    def onSlide(self, evt):
        offset = self.slider.GetValue()
        self.player.Seek(offset)
        
    #Volume Up
    def onVolup(self, evt):
        volume = float(self.player.GetVolume())
        volume = volume + 0.05
        self.player.SetVolume(volume)
        self.label.SetLabel("Vol: %s" %str(volume,))
        
   #Volume Up
    def onVoldown(self, evt):
        volume = float(self.player.GetVolume())
        volume = volume - 0.05
        self.player.SetVolume(volume)
        self.label.SetLabel("Vol: %s" %str(volume,))
        
    #Loads playList to the List box with name, Format, directory
    def onLoadPlaylist(self, paths):
        #get list of directories with file names eg c:\steve.mp3
        file_list_paths = paths 
        file_list = []
        file_path = dict() #hold filename_ext as keyword and path as value and can be compared to selected file in playlist and get its path as value
        for path_s in file_list_paths : 
            path= path_s.strip()
            directory, filename_and_ext = os.path.split(path)            
            full_tuple = (filename_and_ext, directory, "" )
            file_list.append(full_tuple)
            file_path[filename_and_ext] = path
            
        #Insert values in Col and Rows
        for i in file_list:
            index = self.playlist.InsertStringItem(sys.maxint, i[0])
            self.playlist.SetStringItem(index, 1, i[1])
            self.playlist.SetStringItem(index, 2, i[2])
        # Make dictionary the attribute of the class
        self.file_path = file_path 
        
        
        
    # Get selected from list and set it 
    def onSetPath(self):
        index1 = self.playlist.GetFirstSelected() 
        filename1 = str(self.playlist.GetItemText(index1)) 
        filename = filename1.strip()
        path = self.file_path[filename]
        self.onCheck(path)

app = wx.App(False)
f = MediaPlayer(None, -1, "Elijah Player")
f.Center()
f.Show(True)
app.MainLoop()

Recommended Answers

All 15 Replies

Where can we download all of the bitmaps for the buttons for it?

I have attached the Icons. They are Tango Icons and are free to Download. Just extract to the folder with script following structure
folder\media_advanced.py
folder\icons

I have noticed that whenever i run it onSetPath() never seems to ever get run. Would this be a problem, or is it just me that this is happening to?

Anyone got it working? It gives message that now playing but no sound! what is wrong? Have tried on XP and Vista, and will try on ubuntu.

I think something might be wrong with Loading file and play it
Can someone explain how media ctrl loads file and plays it? May be from there I can find the bug and fix it!

OK, here is my sugestion...please bare in mind i know nothing about Python GUI and event handling so i cannot really provide any sort of code examples.

You need to ensure that the media has loaded correctly before you can play it. So the best way to doing this is to play the sound inside the wx.media.EVT_MEDIA_LOADED event.

Hope that makes sense to you
Chris

Yep Will take a look at that event!
Thanks Sir!

can't see it in wxpy docs, any link or example?

Strange, in onCheck() I did a few testprints ...
print self.player.Load(path) --> True
print self.player.Play() --> False, should be True
print self.player.Length() --> 0, should be > 0

It seems to load the media file correctly.
Actually, when you load an .avi file and press stop the first frame shows.

In my own example code the media player works just fine on XP.

Your own code? Can you post them? Have you noticed what is wrong??

As I read through my own code, I figured out your problem. You need to put a small delay in, so the file can be fully read before the player starts ...

# create a simple media player with wxPython's
# wx.lib.filebrowsebutton.FileBrowseButton(parent,labelText,fileMask)
# wx.media.MediaCtrl(parent, id, pos, size, style, szBackend)
# tested with Python25 and wxPython28
# by  vegaseat   12nov2008

import wx
import wx.media
import wx.lib.filebrowsebutton

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, 
            size=mysize)
        self.panel = wx.Panel(self, wx.ID_ANY)
        tan = '#D2B48C'
        self.panel.SetBackgroundColour('tan')
        
        # check if the media control is implemented
        try:
            self.mc = wx.media.MediaCtrl(self.panel)
        except NotImplementedError:
            self.Destroy()
        
        # this file browser masked to look for media files
        mask = "*.mp3;*.mpg;*.mid;*.wav;*.au;*.avi" 
        self.fbb = wx.lib.filebrowsebutton.FileBrowseButton(self.panel,
            labelText="Select a media file:", fileMask=mask)
        load_button = wx.Button(self.panel, wx.ID_ANY, " Load ")
        load_button.SetToolTip(wx.ToolTip("Load file and play"))
        self.Bind(wx.EVT_BUTTON, self.onFileLoad, load_button)
        
        # layout ...
        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        hsizer.Add(self.fbb, 1, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL)
        hsizer.Add(load_button, 0, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL)
        # also creates a border space
        vsizer = wx.BoxSizer(wx.VERTICAL)
        vsizer.Add(hsizer, 1, wx.EXPAND|wx.ALL, border=10)
        vsizer.Add(self.mc, 5, wx.EXPAND|wx.ALL, border=10)
        self.panel.SetSizer(vsizer)
        # this activates the full player with its controls
        # (beats me why?)
        self.mc.Load("")
        
    def onFileLoad(self, event):
        self.path = self.fbb.GetValue()
        #print self.path  # test only
        self.mc.Load(self.path)
        # this small delay is needed to allow the file to load
        wx.FutureCall(500, self.onPlay)
        
    def onPlay(self):
        self.mc.Play()


app = wx.App(0)
# create a MyFrame instance and show the frame
MyFrame(None, "Simple Media Player", (600, 400)).Show()
app.MainLoop()

doesn't play file in my Machine!
Is something wrong with it?

Make the delay bigger, thats the idea behind wx.media.MEDIA_LOADED event it triggers when the media is loaded and only then, unfortunately it seems poorly documented.

Chris

I at last solved it!
I added event to ListCtrl when the track get selected it calls setpath which calls oncheck which Loads the file. I have left onplay only for playing files

Thanks all for your contributions, I really appreciate!
Bugfixing is very challenging!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.