]> git.saurik.com Git - wxWidgets.git/blob - samples/mediaplayer/mediaplayer.cpp
Fixes for wxPython site-packages location.
[wxWidgets.git] / samples / mediaplayer / mediaplayer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mediactrltest.
3 // Purpose: wxMediaCtrl sample
4 // Author: Ryan Norton
5 // Modified by:
6 // Created: 11/10/04
7 // RCS-ID: $Id$
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 // ----------------------------------------------------------------------------
34 // resources
35 // ----------------------------------------------------------------------------
36
37 // the application icon (under Windows and OS/2 it is in resources and even
38 // though we could still include the XPM here it would be unused)
39 #if !defined(__WXMSW__) && !defined(__WXPM__)
40 // #include "../sample.xpm"
41 #endif
42
43 #include "wx/mediactrl.h"
44 #include "wx/filedlg.h"
45 #include "wx/slider.h"
46 #include "wx/sizer.h"
47
48 #include "wx/timer.h"
49
50
51 #if !wxUSE_MEDIACTRL
52 #error "wxUSE_MEDIACTRL must be enabled to use this sample!"
53 #endif
54
55 // ----------------------------------------------------------------------------
56 // private classes
57 // ----------------------------------------------------------------------------
58
59 // Define a new application type, each program should derive a class from wxApp
60 class MyApp : public wxApp
61 {
62 public:
63 // override base class virtuals
64 // ----------------------------
65
66 // this one is called on application startup and is a good place for the app
67 // initialization (doing it here and not in the ctor allows to have an error
68 // return: if OnInit() returns false, the application terminates)
69 virtual bool OnInit();
70 };
71
72
73
74 // Define a new frame type: this is going to be our main frame
75 class MyFrame : public wxFrame
76 {
77 public:
78 // ctor(s)
79 MyFrame(const wxString& title);
80 ~MyFrame();
81
82 // event handlers (these functions should _not_ be virtual)
83 void OnQuit(wxCommandEvent& event);
84 void OnAbout(wxCommandEvent& event);
85 void OnLoop(wxCommandEvent& event);
86
87 void OnOpenFile(wxCommandEvent& event);
88 void OnOpenURL(wxCommandEvent& event);
89
90 void OnPlay(wxCommandEvent& event);
91 void OnPause(wxCommandEvent& event);
92 void OnStop(wxCommandEvent& event);
93
94 void OnSeek(wxCommandEvent& event);
95
96 void OnMediaFinished(wxMediaEvent& event);
97
98 private:
99 void ResetStatus()
100 {
101 m_basestatus = wxString::Format(_T("Size(x,y):%i,%i Length(Seconds):%u Speed:%1.1fx"),
102 m_movie->GetBestSize().x,
103 m_movie->GetBestSize().y,
104 m_movie->GetDuration() / 1000,
105 m_movie->GetPlaybackRate()
106 );
107
108 m_slider->SetRange(0, m_movie->GetDuration() / 1000);
109 }
110
111 wxMediaCtrl* m_movie;
112 wxSlider* m_slider;
113 wxBoxSizer* m_sizer;
114 class MyTimer* m_timer;
115 friend class MyTimer;
116 wxString m_basestatus;
117
118 bool m_bLoop;
119
120 // any class wishing to process wxWidgets events must use this macro
121 DECLARE_EVENT_TABLE()
122 };
123
124 const wxChar* wxGetMediaStateText(int nState)
125 {
126 switch(nState)
127 {
128 case wxMEDIASTATE_PLAYING:
129 return wxT("Playing");
130 case wxMEDIASTATE_STOPPED:
131 return wxT("Stopped");
132 ///case wxMEDIASTATE_PAUSED:
133 default:
134 return wxT("Paused");
135 }
136 }
137
138 class MyTimer : public wxTimer
139 {
140 public:
141 MyTimer(MyFrame* frame) {m_frame = frame;}
142
143 void Notify()
144 {
145 long lPosition = m_frame->m_movie->GetPosition() / 1000;
146 m_frame->m_slider->SetValue(lPosition);
147
148
149 m_frame->SetStatusText(wxString::Format(_T("%s Pos:%u State:%s"),
150 m_frame->m_basestatus.c_str(),
151 lPosition,
152 wxGetMediaStateText(m_frame->m_movie->GetState())
153 )
154 );
155
156 }
157
158 MyFrame* m_frame;
159 };
160
161
162 // ----------------------------------------------------------------------------
163 // constants
164 // ----------------------------------------------------------------------------
165
166 // IDs for the controls and the menu commands
167 enum
168 {
169 // menu items
170 Minimal_Quit = wxID_EXIT,
171
172 // it is important for the id corresponding to the "About" command to have
173 // this standard value as otherwise it won't be handled properly under Mac
174 // (where it is special and put into the "Apple" menu)
175 Minimal_About = wxID_ABOUT,
176
177 Minimal_Slider = 1,
178 Minimal_Media,
179 Minimal_Loop,
180 Minimal_OpenFile,
181 Minimal_OpenURL,
182 Minimal_Play,
183 Minimal_Pause,
184 Minimal_Stop
185 };
186
187 // ----------------------------------------------------------------------------
188 // event tables and other macros for wxWidgets
189 // ----------------------------------------------------------------------------
190
191 // the event tables connect the wxWidgets events with the functions (event
192 // handlers) which process them. It can be also done at run-time, but for the
193 // simple menu events like this the static method is much simpler.
194 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
195 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
196 EVT_MENU(Minimal_About, MyFrame::OnAbout)
197 EVT_MENU(Minimal_Loop, MyFrame::OnLoop)
198 EVT_MENU(Minimal_OpenFile, MyFrame::OnOpenFile)
199 EVT_MENU(Minimal_OpenURL, MyFrame::OnOpenURL)
200 EVT_MENU(Minimal_Play, MyFrame::OnPlay)
201 EVT_MENU(Minimal_Pause, MyFrame::OnPause)
202 EVT_MENU(Minimal_Stop, MyFrame::OnStop)
203 EVT_SLIDER(Minimal_Slider, MyFrame::OnSeek)
204 EVT_MEDIA_FINISHED(Minimal_Media, MyFrame::OnMediaFinished)
205 END_EVENT_TABLE()
206
207 // Create a new application object: this macro will allow wxWidgets to create
208 // the application object during program execution (it's better than using a
209 // static object for many reasons) and also implements the accessor function
210 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
211 // not wxApp)
212 IMPLEMENT_APP(MyApp)
213
214 // ============================================================================
215 // implementation
216 // ============================================================================
217
218 // ----------------------------------------------------------------------------
219 // the application class
220 // ----------------------------------------------------------------------------
221
222 // 'Main program' equivalent: the program execution "starts" here
223 bool MyApp::OnInit()
224 {
225 // create the main application window
226 MyFrame *frame = new MyFrame(_T("Minimal wxWidgets App"));
227
228 // and show it (the frames, unlike simple controls, are not shown when
229 // created initially)
230 frame->Show(true);
231
232 // success: wxApp::OnRun() will be called which will enter the main message
233 // loop and the application will run. If we returned false here, the
234 // application would exit immediately.
235 return true;
236 }
237
238 // ----------------------------------------------------------------------------
239 // main frame
240 // ----------------------------------------------------------------------------
241
242 // frame constructor
243 MyFrame::MyFrame(const wxString& title)
244 : wxFrame(NULL, wxID_ANY, title), m_timer(NULL)
245 {
246 // set the frame icon
247 // SetIcon(wxICON(sample));
248
249 #if wxUSE_MENUS
250 // create a menu bar
251 wxMenu *menuFile = new wxMenu;
252
253 // the "About" item should be in the help menu
254 wxMenu *helpMenu = new wxMenu;
255 helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
256
257 menuFile->Append(Minimal_OpenFile, _T("&Open File"), _T("Open a File"));
258 menuFile->Append(Minimal_OpenURL, _T("Open &URL"), _T("Open a URL"));
259 menuFile->AppendSeparator();
260 menuFile->Append(Minimal_Play, _T("&Play"), _T("Resume playback"));
261 menuFile->Append(Minimal_Pause, _T("P&ause"), _T("Pause playback"));
262 menuFile->Append(Minimal_Stop, _T("&Stop"), _T("Stop playback"));
263 menuFile->AppendSeparator();
264 menuFile->AppendCheckItem(Minimal_Loop, _T("&Loop"), _T("Loop Selected Media"));
265 menuFile->AppendSeparator();
266 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
267
268 // now append the freshly created menu to the menu bar...
269 wxMenuBar *menuBar = new wxMenuBar();
270 menuBar->Append(menuFile, _T("&File"));
271 menuBar->Append(helpMenu, _T("&Help"));
272
273 // ... and attach this menu bar to the frame
274 SetMenuBar(menuBar);
275 #endif // wxUSE_MENUS
276
277 m_sizer = new wxBoxSizer(wxVERTICAL);
278 this->SetSizer(m_sizer);
279 this->SetAutoLayout(true);
280
281 // m_sizer->SetSizeHints(this);
282 // m_sizer->Fit(this);
283
284 m_movie = new wxMediaCtrl(this, Minimal_Media, wxT(""));
285 m_sizer->Add(m_movie, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
286
287 m_slider = new wxSlider(this, Minimal_Slider, 0, //init
288 0, //start
289 0, //end
290 wxDefaultPosition, wxDefaultSize,
291 wxSL_HORIZONTAL );
292 m_sizer->Add(m_slider, 0, wxALIGN_CENTER_HORIZONTAL|wxALL|wxEXPAND , 5);
293
294 wxBoxSizer* horzsizer = new wxBoxSizer(wxHORIZONTAL);
295 m_sizer->Add(horzsizer, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
296
297 m_bLoop = false;
298
299 m_timer = new MyTimer(this);
300 m_timer->Start(100);
301
302 #if wxUSE_STATUSBAR
303 // create a status bar just for fun (by default with 1 pane only)
304 CreateStatusBar(1);
305 ResetStatus();
306 SetStatusText(m_basestatus);
307 #endif // wxUSE_STATUSBAR
308 }
309
310 MyFrame::~MyFrame()
311 {
312 if (m_timer)
313 delete m_timer;
314 }
315
316 // event handlers
317
318 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
319 {
320 // true is to force the frame to close
321 Close(true);
322 }
323
324 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
325 {
326 wxString msg;
327 msg.Printf( _T("This is a test of wxMediaCtrl.\n")
328 _T("Welcome to %s"), wxVERSION_STRING);
329
330 wxMessageBox(msg, _T("About wxMediaCtrl test"), wxOK | wxICON_INFORMATION, this);
331 }
332
333 void MyFrame::OnLoop(wxCommandEvent& WXUNUSED(event))
334 {
335 m_bLoop = !m_bLoop;
336 }
337
338 void MyFrame::OnOpenFile(wxCommandEvent& WXUNUSED(event))
339 {
340 wxFileDialog fd(this);
341
342 if(fd.ShowModal() == wxID_OK)
343 {
344 if( !m_movie->Load(fd.GetPath()) )
345 wxMessageBox(wxT("Couldn't load file!"));
346
347 if( !m_movie->Play() )
348 wxMessageBox(wxT("Couldn't play movie!"));
349
350 ResetStatus();
351 }
352 }
353
354 #include "wx/textdlg.h"
355
356 void MyFrame::OnOpenURL(wxCommandEvent& WXUNUSED(event))
357 {
358 wxString theURL = wxGetTextFromUser(wxT("Enter the URL that has the movie to play"));
359
360 if(!theURL.empty())
361 {
362 if( !m_movie->Load(wxURI(theURL)) )
363 wxMessageBox(wxT("Couldn't load URL!"));
364
365 if( !m_movie->Play() )
366 wxMessageBox(wxT("Couldn't play movie!"));
367
368 ResetStatus();
369 }
370 }
371
372 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
373 {
374 if( !m_movie->Play() )
375 wxMessageBox(wxT("Couldn't play movie!"));
376 }
377
378 void MyFrame::OnPause(wxCommandEvent& WXUNUSED(event))
379 {
380 if( !m_movie->Pause() )
381 wxMessageBox(wxT("Couldn't pause movie!"));
382 }
383
384 void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
385 {
386 if( !m_movie->Stop() )
387 wxMessageBox(wxT("Couldn't stop movie!"));
388 }
389
390 void MyFrame::OnSeek(wxCommandEvent& WXUNUSED(event))
391 {
392 if( !m_movie->SetPosition( m_slider->GetValue() * 1000 ) )
393 wxMessageBox(wxT("Couldn't seek in movie!"));
394 }
395
396 void MyFrame::OnMediaFinished(wxMediaEvent& WXUNUSED(event))
397 {
398 if(m_bLoop)
399 {
400 if ( !m_movie->SetPosition(0) || !m_movie->Play() )
401 wxMessageBox(wxT("Couldn't seek or play to loop movie!"));
402 }
403 }