More Controls/Windows → MediaCtrl
import wx
import wx.media
import os
ID_Load = wx.NewId()
ID_Play = wx.NewId()
class Frame(wx.Frame):
def __init__(
self, parent=None, id=wx.ID_ANY, title='MediaCtrl',
pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE
):
wx.Frame.__init__(self, parent, id, title, pos, size, style)
menuBar = wx.MenuBar()
fileMenu = wx.Menu()
fileMenu.Append(ID_Load, 'Load')
fileMenu.Append(ID_Play, 'Play')
menuBar.Append(fileMenu, 'File')
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnLoadFile, id=ID_Load)
self.Bind(wx.EVT_MENU, self.OnPlay, id=ID_Play)
self.panel = wx.Panel(self, wx.ID_ANY)
try:
self.mediaCtrl = wx.media.MediaCtrl(self.panel, style=wx.SIMPLE_BORDER)
except NotImplementError:
self.Destroy()
raise
sizer = wx.BoxSizer()
sizer.Add(self.mediaCtrl, 1, wx.EXPAND)
self.panel.SetSizer(sizer)
self.Bind(wx.media.EVT_MEDIA_LOADED, self.OnMediaLoaded)
self.Bind(wx.media.EVT_MEDIA_FINISHED, self.OnMediaFinished)
self.Bind(wx.media.EVT_MEDIA_PAUSE, self.OnMediaPause)
self.Bind(wx.media.EVT_MEDIA_PLAY, self.OnMediaPlay)
self.Bind(wx.media.EVT_MEDIA_STOP, self.OnMediaStop)
self.Bind(wx.media.EVT_MEDIA_STATECHANGED, self.OnStateChanged)
def OnLoadFile(self, event):
fileDlg = wx.FileDialog(self, message='Choose a file',
defaultDir=os.getcwd(),
wildcard='Media file(*.*)|*.*',
style=wx.OPEN | wx.CHANGE_DIR)
if fileDlg.ShowModal() == wx.ID_OK:
path = fileDlg.GetPath()
self.LoadFile(path)
def OnPlay(self, event):
if not self.mediaCtrl.Play():
wx.MessageBox('Unable to play %s!', 'Prompt')
else:
self.mediaCtrl.SetInitialSize()
self.panel.GetSizer().Layout()
def OnMediaLoaded(self, event):
pass
def OnMediaFinished(self, event):
pass
def OnMediaPause(self, event):
pass
def OnMediaPlay(self, event):
pass
def OnMediaStop(self, event):
pass
def OnStateChanged(self, event):
pass
def LoadFile(self, path):
if not self.mediaCtrl.Load(path):
wx.MessageBox('Unable to load %s1' % path, 'Prompt')
else:
self.mediaCtrl.SetInitialSize()
self.panel.GetSizer().Layout()
def TestFrame():
app = wx.PySimpleApp()
frame = Frame()
frame.Centre()
frame.Show()
app.MainLoop()
if __name__ == '__main__':
TestFrame()