945,063 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 10065
  • Python RSS
2

wxPython Plays Audio and Video Files

by on Mar 10th, 2006
An example showing how to use the wx.media.MediaCtrl() widget to play MIDI, MP3, WAV, AU, AVI and MPG audio and video files. I have only tested it with Windows. If you are very ambitious, you could use a random graphics display with the sound, or tie the slider position to a slide presentation.
Python Code Snippet (Toggle Plain Text)
  1. # experiment with wxPython's
  2. # wx.media.MediaCtrl(parent, id, pos, size, style, backend)
  3. # the backend (szBackend) is usually figured by the control
  4. # wxMEDIABACKEND_DIRECTSHOW Windows
  5. # wxMEDIABACKEND_QUICKTIME Mac OS X
  6. # wxMEDIABACKEND_GSTREAMER Linux (?)
  7. # plays files with extension .mid .mp3 .wav .au .avi .mpg
  8. # tested with Python24 and wxPython26 on Windows XP vegaseat 10mar2006
  9.  
  10. import wx
  11. import wx.media
  12. import os
  13.  
  14. class Panel1(wx.Panel):
  15. def __init__(self, parent, id):
  16. #self.log = log
  17. wx.Panel.__init__(self, parent, -1, style=wx.TAB_TRAVERSAL|wx.CLIP_CHILDREN)
  18.  
  19. # Create some controls
  20. try:
  21. self.mc = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER)
  22. except NotImplementedError:
  23. self.Destroy()
  24. raise
  25.  
  26. loadButton = wx.Button(self, -1, "Load File")
  27. self.Bind(wx.EVT_BUTTON, self.onLoadFile, loadButton)
  28.  
  29. playButton = wx.Button(self, -1, "Play")
  30. self.Bind(wx.EVT_BUTTON, self.onPlay, playButton)
  31.  
  32. pauseButton = wx.Button(self, -1, "Pause")
  33. self.Bind(wx.EVT_BUTTON, self.onPause, pauseButton)
  34.  
  35. stopButton = wx.Button(self, -1, "Stop")
  36. self.Bind(wx.EVT_BUTTON, self.onStop, stopButton)
  37.  
  38. slider = wx.Slider(self, -1, 0, 0, 0, size=wx.Size(300, -1))
  39. self.slider = slider
  40. self.Bind(wx.EVT_SLIDER, self.onSeek, slider)
  41.  
  42. self.st_file = wx.StaticText(self, -1, ".mid .mp3 .wav .au .avi .mpg", size=(200,-1))
  43. self.st_size = wx.StaticText(self, -1, size=(100,-1))
  44. self.st_len = wx.StaticText(self, -1, size=(100,-1))
  45. self.st_pos = wx.StaticText(self, -1, size=(100,-1))
  46.  
  47. # setup the button/label layout using a sizer
  48. sizer = wx.GridBagSizer(5,5)
  49. sizer.Add(loadButton, (1,1))
  50. sizer.Add(playButton, (2,1))
  51. sizer.Add(pauseButton, (3,1))
  52. sizer.Add(stopButton, (4,1))
  53. sizer.Add(self.st_file, (1, 2))
  54. sizer.Add(self.st_size, (2, 2))
  55. sizer.Add(self.st_len, (3, 2))
  56. sizer.Add(self.st_pos, (4, 2))
  57. sizer.Add(self.mc, (5,1), span=(5,1)) # for .avi .mpg video files
  58. self.SetSizer(sizer)
  59.  
  60. self.timer = wx.Timer(self)
  61. self.Bind(wx.EVT_TIMER, self.onTimer)
  62. self.timer.Start(100)
  63.  
  64. def onLoadFile(self, evt):
  65. dlg = wx.FileDialog(self, message="Choose a media file",
  66. defaultDir=os.getcwd(), defaultFile="",
  67. style=wx.OPEN | wx.CHANGE_DIR )
  68. if dlg.ShowModal() == wx.ID_OK:
  69. path = dlg.GetPath()
  70. self.doLoadFile(path)
  71. dlg.Destroy()
  72.  
  73. def doLoadFile(self, path):
  74. if not self.mc.Load(path):
  75. wx.MessageBox("Unable to load %s: Unsupported format?" % path, "ERROR", wx.ICON_ERROR | wx.OK)
  76. else:
  77. folder, filename = os.path.split(path)
  78. self.st_file.SetLabel('%s' % filename)
  79. self.mc.SetBestFittingSize()
  80. self.GetSizer().Layout()
  81. self.slider.SetRange(0, self.mc.Length())
  82. self.mc.Play()
  83.  
  84. def onPlay(self, evt):
  85. self.mc.Play()
  86.  
  87. def onPause(self, evt):
  88. self.mc.Pause()
  89.  
  90. def onStop(self, evt):
  91. self.mc.Stop()
  92.  
  93. def onSeek(self, evt):
  94. offset = self.slider.GetValue()
  95. self.mc.Seek(offset)
  96.  
  97. def onTimer(self, evt):
  98. offset = self.mc.Tell()
  99. self.slider.SetValue(offset)
  100. self.st_size.SetLabel('size: %s ms' % self.mc.Length())
  101. self.st_len.SetLabel('( %d seconds )' % (self.mc.Length()/1000))
  102. self.st_pos.SetLabel('position: %d ms' % offset)
  103.  
  104.  
  105. app = wx.PySimpleApp()
  106. # create a window/frame, no parent, -1 is default ID
  107. frame = wx.Frame(None, -1, "play audio and video files", size = (320, 350))
  108. # call the derived class
  109. Panel1(frame, -1)
  110. frame.Show(1)
  111. app.MainLoop()
Comments on this Code Snippet
Jul 16th, 2009
0

Re: wxPython Plays Audio and Video Files

The slider doesnt work, any idea how to make the slider work?
Newbie Poster
ananthram90 is offline Offline
1 posts
since Jul 2009
Feb 15th, 2010
0

Re: wxPython Plays Audio and Video Files

heya, this is a reli good piece of work, but im having a bit of a problem.. the actual video doesn't appear when i pressed play, only the sound was playing, but if I resize the window (like dragging the edge to stretch the window), the video displays straight away... did this happen to anyone else? Is this possibly a bug with the new python 2.6? Any suggestions/advices on how to fix this?
Thanks ever so much
Newbie Poster
wise_stam is offline Offline
1 posts
since Feb 2010
Jun 23rd, 2010
0

Re: wxPython Plays Audio and Video Files

ya the play problem is with me too
were u able to fix it? if yes how?
may b some problem with size i guess

n about slider
Python Syntax (Toggle Plain Text)
  1. slider = wx.Slider(self, -1, 0,0.0001,3000, pos=(120,680), style = wx.SL_HORIZONTAL | wx.SL_LABELS, size = (400, -1))

is working well, just dont write 0,0,0 as 3rd , fourth n fifth aurgument.. and 5th onle shud b long enuf till length of ur video!
Newbie Poster
sarosh is offline Offline
8 posts
since Feb 2010
Message:
Previous Thread in Python Forum Timeline: Attribute error SPSS/Python-Script (reading text from output))
Next Thread in Python Forum Timeline: Multi-line output to a single line output





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC