Dazu ist pysonic auch noch sehr alt. Ich schlage vor wxPython zu verwenden. Hier ist ein Beispiel:
Code: Alles auswählen
# experiment with wxPython's
# wx.media.MediaCtrl(parent, id, pos, size, style, backend)
# the correct backend is usually figured by the control
# wxMEDIABACKEND_DIRECTSHOW Windows
# wxMEDIABACKEND_QUICKTIME Mac OS X
# wxMEDIABACKEND_GSTREAMER Linux
# plays .mid .mp3 .wav .au .avi .mpg
import wx
import wx.media
import os
class MyPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, -1,
style=wx.TAB_TRAVERSAL|wx.CLIP_CHILDREN)
try:
self.mc = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER)
except NotImplementedError:
self.Destroy()
raise
loadButton = wx.Button(self, -1, "Load File")
self.Bind(wx.EVT_BUTTON, self.onLoadFile, loadButton)
playButton = wx.Button(self, -1, "Play")
self.Bind(wx.EVT_BUTTON, self.onPlay, playButton)
pauseButton = wx.Button(self, -1, "Pause")
self.Bind(wx.EVT_BUTTON, self.onPause, pauseButton)
stopButton = wx.Button(self, -1, "Stop")
self.Bind(wx.EVT_BUTTON, self.onStop, stopButton)
ext = ".mid .mp3 .wav .au .avi .mpg"
self.st_file = wx.StaticText(self, -1, ext, size=(200,-1))
self.st_size = wx.StaticText(self, -1, size=(100,-1))
self.st_len = wx.StaticText(self, -1, size=(100,-1))
self.st_pos = wx.StaticText(self, -1, size=(100,-1))
# setup the button and label layout using a sizer
sizer = wx.GridBagSizer(5,5)
sizer.Add(loadButton, (1,1))
sizer.Add(playButton, (2,1))
sizer.Add(pauseButton, (3,1))
sizer.Add(stopButton, (4,1))
sizer.Add(self.st_file, (1, 2))
sizer.Add(self.st_size, (2, 2))
sizer.Add(self.st_len, (3, 2))
sizer.Add(self.st_pos, (4, 2))
# for .avi .mpg video files
sizer.Add(self.mc, (5,1), span=(5,1))
self.SetSizer(sizer)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.onTimer)
self.timer.Start(100)
def onLoadFile(self, evt):
dlg = wx.FileDialog(self, message="Choose a media file",
defaultDir=os.getcwd(), defaultFile="",
style=wx.OPEN|wx.CHANGE_DIR )
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
self.doLoadFile(path)
dlg.Destroy()
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.st_file.SetLabel('%s' % filename)
self.GetSizer().Layout()
self.mc.Play()
def onPlay(self, evt):
self.mc.Play()
def onPause(self, evt):
self.mc.Pause()
def onStop(self, evt):
self.mc.Stop()
def onTimer(self, evt):
offset = self.mc.Tell()
self.st_size.SetLabel('size: %s ms' % self.mc.Length())
#self.st_len.SetLabel('( %d seconds )' % (self.mc.Length()/1000))
self.st_pos.SetLabel('position: %d ms' % offset)
app = wx.PySimpleApp()
# create a frame, no parent, -1 is default ID
frame = wx.Frame(None, -1, "play audio and video files", size = (320, 350))
MyPanel(frame, -1)
frame.Show(True)
app.MainLoop()