How to make a song play automatically in WxPython MediaCtrl?

I create a music player, and everything goes well, if you do not press the next button, it downloads the next song, but will not play it if the user does not press the play button. In any case, to make this game automatically, simply by pressing the next button and not pressing the play button after that. I tried to bind the playback function and the download function to the button, and it didn’t work, I also tried just to include mc.Play () in my load function, but this also does not work.

Here is my code:

import wx import wx.media import os class MainWindow(wx.Frame): def __init__(self,parent,id): wx.Frame.__init__(self,parent,id,'Music Player',size=(900,670),style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN) wx.Frame.CenterOnScreen(self) bg = wx.Image('bg.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.bitmap1 = wx.StaticBitmap(self, -1, bg, (0,0)) panel = wx.Panel(self,style=wx.STAY_ON_TOP) ##MENU AND STATUS BAR self.status = self.CreateStatusBar() self.status.SetStatusText('Ready') menubar = wx.MenuBar() file_menu = wx.Menu() view_menu = wx.Menu() controls_menu = wx.Menu() help_menu = wx.Menu() #MENU ID'S ID_FILE_LOAD = 2 ID_FILE_EXIT = 3 ID_VIEW_SHOW_STATUSBAR = 4 ID_CONTROLS_PLAY = 5 ID_CONTROLS_PAUSE = 6 ID_CONTROLS_STOP = 7 ID_HELP_ABOUT = 8 ##FILE MENU file_menu.Append(ID_FILE_LOAD, "&Load...\tCtrl+L", "This will let you choose a song to load") file_menu.AppendSeparator() file_menu.Append(ID_FILE_EXIT,"Exit","This will exit the program") ##VIEW MENU self.check_statusbar = view_menu.Append(ID_VIEW_SHOW_STATUSBAR,'Show Stat&usbar\tCtrl+U', "This will disable the statusbar", kind=wx.ITEM_CHECK) view_menu.Check(self.check_statusbar.GetId(), True) ##CONTROLS MENU controls_menu.Append(ID_CONTROLS_PLAY,"&Play\tEnter", "Play the selected song") controls_menu.Append(ID_CONTROLS_PAUSE,"&Pause\tSpace", "Pause the selected song") ##MENUBAR APPEND menubar.Append(file_menu,"File") menubar.Append(view_menu,"View") menubar.Append(controls_menu,"Controls") menubar.Append(help_menu,"Help") self.SetMenuBar(menubar) ##MENU ACTION BINDING self.Bind(wx.EVT_MENU, self.Load, None, 2) self.Bind(wx.EVT_MENU, self.Close, None, 3) self.Bind(wx.EVT_MENU, self.ToggleStatusBar, self.check_statusbar) self.Bind(wx.EVT_MENU, self.Play, None, 5) self.Bind(wx.EVT_MENU, self.Pause, None, 6) self.Bind(wx.EVT_MENU, self.About, None, 8) ##FONTS font1 = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD) font2 = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD) try: self.mc = wx.media.MediaCtrl(self) except NotImplementedError: raise ##BUTTONS bttnprt = self.bitmap1 loadButton = wx.Button(bttnprt, -1, "Load File...", pos=(308,435), size=(281,31)) self.Bind(wx.EVT_BUTTON, self.Load, loadButton) playButton = wx.Button(bttnprt, -1, "Play", pos=(458,491), size=(57,57)) self.Bind(wx.EVT_BUTTON, self.Play, playButton) pauseButton = wx.Button(bttnprt, -1, "Pause", pos=(383,491), size=(57,57)) self.Bind(wx.EVT_BUTTON, self.Pause, pauseButton) volumeUpButton = wx.Button(bttnprt, -1, "Up", pos=(400,299), size=(95,52)) self.Bind(wx.EVT_BUTTON, self.onSetVolumeUp, volumeUpButton) volumeDownButton = wx.Button(bttnprt, -1, "Down", pos=(400,363), size=(95,52)) self.Bind(wx.EVT_BUTTON, self.onSetVolumeDown, volumeDownButton) backButton = wx.Button(bttnprt, -1, "Back", pos=(308,491), size=(57,57)) self.Bind(wx.EVT_BUTTON, self.previousSong, backButton) nextButton = wx.Button(bttnprt, -1, "Next", pos=(533,491), size=(57,57)) self.Bind(wx.EVT_BUTTON, self.nextSong, nextButton) self.volumeCtrl = wx.Slider(bttnprt, value=50, minValue=0, maxValue=100, style=wx.SL_VERTICAL|wx.SL_INVERSE, pos=(300,300)) # self.volumeCtrl.Bind(wx.EVT_SLIDER, self.onSetVolume) self.volumeCtrl.Hide() songlist = os.listdir('songs') self.myListBox = listbox = wx.ListBox(bttnprt, -1, (301,80), (296,206), songlist, wx.LB_SINGLE) self.Bind(wx.EVT_LISTBOX, self.selLoadFile, listbox) # self.st_file = wx.StaticText(bttnprt, -1, "Blank", pos=(30,30)) def newWin(self, event): self.new = NewWindow(parent=self, id=-1) self.new.Show() def Close(self, event): box=wx.MessageDialog(None, 'Are you sure you want to exit?', 'Exit program?', wx.YES_NO) answer=box.ShowModal() if answer==wx.ID_YES: self.Destroy() def About(self, event): self.new = AboutWindow(parent=self, id=-1) self.new.Show() def selLoadFile(self, event): my_selection = self.myListBox.GetStringSelection() file_path = os.path.join(os.getcwd(),"songs",my_selection) self.doLoadFile2(file_path) def Load(self, event): dlg = wx.FileDialog(self, "Choose a media file", "songs", "", "*.*", wx.OPEN) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() self.doLoadFile(path) dlg.Destroy() def load2(self): my_selection = self.myListBox.GetStringSelection() file_path = os.path.join(os.getcwd(),"songs",my_selection) self.doLoadFile2(file_path) self.mc.Play() def doLoadFile(self, path): if not self.mc.Load(path): wx.MessageBox("Unable to load %s: Unsupported format?" % path, "ERROR", wx.ICON_ERROR | wx.OK) else: folder, filename = os.path.split(path) self.mc.SetBestFittingSize() self.mc.Play() def doLoadFile2(self, file_path): if not self.mc.Load(file_path): wx.MessageBox("Unable to load %s: Unsupported format?" % file_path, "ERROR", wx.ICON_ERROR | wx.OK) else: folder, filename = os.path.split(file_path) #self.st_file.SetLabel('%s' % filename) self.status.SetStatusText("Now Playing: " +'%s' % filename) self.mc.SetBestFittingSize() self.mc.Play() def Play(self, event): self.mc.Play() def Pause(self, event): self.mc.Pause() def onSetVolumeUp(self, event): self.currentVolume = self.volumeCtrl.GetValue() self.newVolumeAdd = self.currentVolume + 1.5 self.volumeCtrl.SetValue(self.newVolumeAdd) self.mc.SetVolume(float(self.currentVolume) / 100) def onSetVolumeDown(self, event): self.currentVolume = self.volumeCtrl.GetValue() self.newVolumeSub = self.currentVolume - 1.5 self.volumeCtrl.SetValue(self.newVolumeSub) self.mc.SetVolume(float(self.currentVolume) / 100) def previousSong(self, event): current = self.myListBox.GetSelection() new = current - 1 self.myListBox.SetSelection(new) self.mc.Stop() self.load2() def nextSong(self, event): current = self.myListBox.GetSelection() new = current + 1 self.myListBox.SetSelection(new) self.mc.Stop() self.load2() def ToggleStatusBar(self, e): if self.check_statusbar.IsChecked(): self.status.Show() self.status.SetStatusText('Ready') else: self.status.Hide() ##RUN## if __name__=='__main__': app=wx.PySimpleApp() frame=MainWindow(parent=None,id=-1) frame.Show() app.MainLoop() 
+4
source share
1 answer

If it is windows and possibly specifically Win 7, 64bit, you need to do 2 things to make it work.

  • As noted by J. F. Sebastian, you need to call the game from the method associated with the EVT_MEDIA_LOADED event. docs really mention it.
  • Define the backend when creating MediaCtrl. I'm not sure why this is necessary, but it did not work for me until I did this.

So try the following changes:

 ... #change this existing line self.mc = wx.media.MediaCtrl(self, szBackend=wx.media.MEDIABACKEND_WMP10) ... # add this new line self.Bind(wx.media.EVT_MEDIA_LOADED, self.song_is_loaded) ... # add this new method def song_is_loaded(self, event): self.mc.Play() 

You can also delete all calls to self.mc.Play (), except for those that are in the Play () function itself (since downloading the file will now make it play).

I also noticed that you defined a bunch of ID_XXX constants, but then just used numbers when you linked your menu buttons.

+2
source

All Articles