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