]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/MediaCtrl.py
Warning fixes and source cleaning.
[wxWidgets.git] / wxPython / demo / MediaCtrl.py
CommitLineData
256c055e
RD
1
2import wx
3import wx.media
4import os
5
6#----------------------------------------------------------------------
7
4617be08
RD
8class StaticText(wx.StaticText):
9 """
10 A StaticText that only updates the label if it has changed, to
11 help reduce potential flicker since these controls would be
12 updated very frequently otherwise.
13 """
14 def SetLabel(self, label):
15 if label <> self.GetLabel():
16 wx.StaticText.SetLabel(self, label)
17
18#----------------------------------------------------------------------
19
256c055e
RD
20class TestPanel(wx.Panel):
21 def __init__(self, parent, log):
22 self.log = log
23 wx.Panel.__init__(self, parent, -1,
24 style=wx.TAB_TRAVERSAL|wx.CLIP_CHILDREN)
25
26 # Create some controls
a7593631 27 try:
6db2ee93
RD
28 self.mc = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER,
29 #szBackend=wx.media.MEDIABACKEND_DIRECTSHOW
30 #szBackend=wx.media.MEDIABACKEND_QUICKTIME
31 #szBackend=wx.media.MEDIABACKEND_WMP10
32 )
a7593631
RD
33 except NotImplementedError:
34 self.Destroy()
35 raise
256c055e 36
4617be08
RD
37 self.Bind(wx.media.EVT_MEDIA_LOADED, self.OnMediaLoaded)
38
256c055e
RD
39 btn1 = wx.Button(self, -1, "Load File")
40 self.Bind(wx.EVT_BUTTON, self.OnLoadFile, btn1)
41
95262b5f
RD
42 btn2 = wx.Button(self, -1, "Play")
43 self.Bind(wx.EVT_BUTTON, self.OnPlay, btn2)
4617be08 44 self.playBtn = btn2
256c055e 45
95262b5f
RD
46 btn3 = wx.Button(self, -1, "Pause")
47 self.Bind(wx.EVT_BUTTON, self.OnPause, btn3)
256c055e 48
95262b5f
RD
49 btn4 = wx.Button(self, -1, "Stop")
50 self.Bind(wx.EVT_BUTTON, self.OnStop, btn4)
51
d6660ffa
RD
52 slider = wx.Slider(self, -1, 0, 0, 0)
53 self.slider = slider
095315e2 54 slider.SetMinSize((150, -1))
d6660ffa
RD
55 self.Bind(wx.EVT_SLIDER, self.OnSeek, slider)
56
4617be08
RD
57 self.st_size = StaticText(self, -1, size=(100,-1))
58 self.st_len = StaticText(self, -1, size=(100,-1))
59 self.st_pos = StaticText(self, -1, size=(100,-1))
d6660ffa
RD
60
61
256c055e
RD
62 # setup the layout
63 sizer = wx.GridBagSizer(5,5)
64 sizer.Add(self.mc, (1,1), span=(5,1))#, flag=wx.EXPAND)
65 sizer.Add(btn1, (1,3))
66 sizer.Add(btn2, (2,3))
67 sizer.Add(btn3, (3,3))
68 sizer.Add(btn4, (4,3))
d6660ffa
RD
69 sizer.Add(slider, (6,1), flag=wx.EXPAND)
70 sizer.Add(self.st_size, (1, 5))
71 sizer.Add(self.st_len, (2, 5))
72 sizer.Add(self.st_pos, (3, 5))
256c055e 73 self.SetSizer(sizer)
d6660ffa 74
4617be08
RD
75 #self.DoLoadFile(os.path.abspath("data/testmovie.mpg"))
76 wx.CallAfter(self.DoLoadFile, os.path.abspath("data/testmovie.mpg"))
d6660ffa
RD
77
78 self.timer = wx.Timer(self)
79 self.Bind(wx.EVT_TIMER, self.OnTimer)
80 self.timer.Start(100)
81
256c055e
RD
82
83
84 def OnLoadFile(self, evt):
85 dlg = wx.FileDialog(self, message="Choose a media file",
86 defaultDir=os.getcwd(), defaultFile="",
87 style=wx.OPEN | wx.CHANGE_DIR )
88 if dlg.ShowModal() == wx.ID_OK:
89 path = dlg.GetPath()
d6660ffa 90 self.DoLoadFile(path)
256c055e 91 dlg.Destroy()
d6660ffa
RD
92
93
94 def DoLoadFile(self, path):
4617be08 95 self.playBtn.Disable()
ddba849c 96 #noLog = wx.LogNull()
d6660ffa
RD
97 if not self.mc.Load(path):
98 wx.MessageBox("Unable to load %s: Unsupported format?" % path,
99 "ERROR",
100 wx.ICON_ERROR | wx.OK)
101 else:
170acdc9 102 self.mc.SetInitialSize()
d6660ffa 103 self.GetSizer().Layout()
095315e2 104 self.slider.SetRange(0, self.mc.Length())
4617be08
RD
105
106 def OnMediaLoaded(self, evt):
107 self.playBtn.Enable()
256c055e 108
256c055e 109 def OnPlay(self, evt):
4617be08
RD
110 if not self.mc.Play():
111 wx.MessageBox("Unable to Play media : Unsupported format?",
112 "ERROR",
113 wx.ICON_ERROR | wx.OK)
114 else:
170acdc9 115 self.mc.SetInitialSize()
8c9126ac 116 self.GetSizer().Layout()
4617be08
RD
117 self.slider.SetRange(0, self.mc.Length())
118
256c055e
RD
119 def OnPause(self, evt):
120 self.mc.Pause()
121
122 def OnStop(self, evt):
123 self.mc.Stop()
124
125
d6660ffa
RD
126 def OnSeek(self, evt):
127 offset = self.slider.GetValue()
128 self.mc.Seek(offset)
129
130 def OnTimer(self, evt):
131 offset = self.mc.Tell()
132 self.slider.SetValue(offset)
133 self.st_size.SetLabel('size: %s' % self.mc.GetBestSize())
134 self.st_len.SetLabel('length: %d seconds' % (self.mc.Length()/1000))
135 self.st_pos.SetLabel('position: %d' % offset)
256c055e 136
ddba849c
RD
137 def ShutdownDemo(self):
138 self.timer.Stop()
139 del self.timer
256c055e
RD
140
141#----------------------------------------------------------------------
142
143def runTest(frame, nb, log):
144 try:
145 win = TestPanel(nb, log)
146 return win
147 except NotImplementedError:
148 from Main import MessagePanel
149 win = MessagePanel(nb, 'wx.MediaCtrl is not available on this platform.',
150 'Sorry', wx.ICON_WARNING)
151 return win
152
153
154#----------------------------------------------------------------------
155
156
157
158overview = """<html><body>
159<h2><center>wx.MediaCtrl</center></h2>
160
d6660ffa
RD
161wx.MediaCtrl is a class that allows a way to convieniently display
162various types of media, such as videos, audio files, natively through
163native codecs. Several different formats of audio and video files are
164supported, but some formats may not be playable on all platforms or
165may require specific codecs to be installed.
9828eeb4
RD
166
167<p>
168wx.MediaCtrl uses native backends to render media, for example on Windows
169there is a ActiveMovie/DirectShow backend, and on Macintosh there is a
170QuickTime backend.
171<p>
172wx.MediaCtrl is not currently available on unix systems.
173
256c055e
RD
174</body></html>
175"""
176
177
178
179if __name__ == '__main__':
180 import sys,os
181 import run
182 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])