1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mediactrltest.
3 // Purpose: wxMediaCtrl sample
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 #include "wx/mediactrl.h" //for wxMediaCtrl
35 #include "wx/filedlg.h" //for opening files from OpenFile
36 #include "wx/slider.h" //for a slider for seeking within media
37 #include "wx/sizer.h" //for positioning controls/wxBoxSizer
38 #include "wx/timer.h" //timer for updating status bar
39 #include "wx/textdlg.h" //for getting user text from OpenURL
43 #error "wxUSE_MEDIACTRL must be enabled to use this sample!"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 class MyApp
: public wxApp
53 virtual bool OnInit();
56 class MyFrame
: public wxFrame
60 MyFrame(const wxString
& title
);
63 // event handlers (these functions should _not_ be virtual)
64 void OnQuit(wxCommandEvent
& event
);
65 void OnAbout(wxCommandEvent
& event
);
66 void OnLoop(wxCommandEvent
& event
);
68 void OnOpenFile(wxCommandEvent
& event
);
69 void OnOpenURL(wxCommandEvent
& event
);
71 void OnPlay(wxCommandEvent
& event
);
72 void OnPause(wxCommandEvent
& event
);
73 void OnStop(wxCommandEvent
& event
);
75 void OnSeek(wxCommandEvent
& event
);
77 void OnMediaFinished(wxMediaEvent
& event
);
82 wxMediaCtrl
* m_mediactrl
;
85 class MyTimer
* m_timer
;
87 wxString m_basestatus
;
91 // any class wishing to process wxWidgets events must use this macro
98 //Here we just make a simple status string
99 //with some useful info about the media
100 //We display info here in seconds (wxMediaCtrl
101 //uses milliseconds - that's why we divide by 1000)
103 void MyFrame
::ResetStatus()
105 m_basestatus
= wxString
::Format(_T("Size(x,y):%i,%i Length(Seconds):%u Speed:%1.1fx"),
106 m_mediactrl
->GetBestSize().x
,
107 m_mediactrl
->GetBestSize().y
,
108 m_mediactrl
->GetDuration() / 1000,
109 m_mediactrl
->GetPlaybackRate()
112 m_slider
->SetRange(0, m_mediactrl
->GetDuration() / 1000);
116 //wxGetMediaStateText
117 //-------------------
118 //Converts a wxMediaCtrl state into something
119 //useful that we can display
121 const wxChar
* wxGetMediaStateText(int nState
)
125 case wxMEDIASTATE_PLAYING
:
126 return wxT("Playing");
127 case wxMEDIASTATE_STOPPED
:
128 return wxT("Stopped");
129 ///case wxMEDIASTATE_PAUSED:
131 return wxT("Paused");
135 class MyTimer
: public wxTimer
138 MyTimer(MyFrame
* frame
) {m_frame
= frame
;}
143 //Updates the main frame's status bar with the current
144 //position within the media and state the media is in
148 long lPosition
= m_frame
->m_mediactrl
->GetPosition() / 1000;
149 m_frame
->m_slider
->SetValue(lPosition
);
152 m_frame
->SetStatusText(wxString
::Format(_T("%s Pos:%u State:%s"),
153 m_frame
->m_basestatus
.c_str(),
155 wxGetMediaStateText(m_frame
->m_mediactrl
->GetState())
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 // IDs for the controls and the menu commands
173 Minimal_Quit
= wxID_EXIT
,
180 Minimal_About
= wxID_ABOUT
,
185 // id for our wxMediaCtrl
189 // ----------------------------------------------------------------------------
190 // event tables and other macros for wxWidgets
191 // ----------------------------------------------------------------------------
193 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
)
205 EVT_SLIDER(Minimal_Slider
, MyFrame
::OnSeek
)
208 EVT_MEDIA_FINISHED(Minimal_Media
, MyFrame
::OnMediaFinished
)
214 // ============================================================================
216 // ============================================================================
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 // 'Main program' equivalent: the program execution "starts" here
225 MyFrame
*frame
= new MyFrame(_T("Minimal wxWidgets App"));
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
238 //Creates our menus and controls
240 MyFrame
::MyFrame(const wxString
& title
)
241 : wxFrame(NULL
, wxID_ANY
, title
), m_timer(NULL
)
247 wxMenu
*menuFile
= new wxMenu
;
249 wxMenu
*helpMenu
= new wxMenu
;
250 helpMenu
->Append(Minimal_About
, _T("&About...\tF1"), _T("Show about dialog"));
252 menuFile
->Append(Minimal_OpenFile
, _T("&Open File"), _T("Open a File"));
253 menuFile
->Append(Minimal_OpenURL
, _T("Open &URL"), _T("Open a URL"));
254 menuFile
->AppendSeparator();
255 menuFile
->Append(Minimal_Play
, _T("&Play"), _T("Resume playback"));
256 menuFile
->Append(Minimal_Pause
, _T("P&ause"), _T("Pause playback"));
257 menuFile
->Append(Minimal_Stop
, _T("&Stop"), _T("Stop playback"));
258 menuFile
->AppendSeparator();
259 menuFile
->AppendCheckItem(Minimal_Loop
, _T("&Loop"), _T("Loop Selected Media"));
260 menuFile
->AppendSeparator();
261 menuFile
->Append(Minimal_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
263 wxMenuBar
*menuBar
= new wxMenuBar();
264 menuBar
->Append(menuFile
, _T("&File"));
265 menuBar
->Append(helpMenu
, _T("&Help"));
268 #endif // wxUSE_MENUS
271 // Create and attach the first/main sizer
274 m_sizer
= new wxBoxSizer(wxVERTICAL
);
275 this->SetSizer(m_sizer
);
276 this->SetAutoLayout(true);
279 // Create our media control
282 m_mediactrl
= new wxMediaCtrl(this, Minimal_Media
, wxT(""));
283 m_sizer
->Add(m_mediactrl
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
289 m_slider
= new wxSlider(this, Minimal_Slider
, 0, //init
292 wxDefaultPosition
, wxDefaultSize
,
294 m_sizer
->Add(m_slider
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
|wxEXPAND
, 5);
298 // Create the second sizer which will position things
301 // -------Menu----------
307 wxBoxSizer
* horzsizer
= new wxBoxSizer(wxHORIZONTAL
);
308 m_sizer
->Add(horzsizer
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
311 // We arn't looping initially
317 // Create our status bar
320 // create a status bar just for fun (by default with 1 pane only)
323 SetStatusText(m_basestatus
);
324 #endif // wxUSE_STATUSBAR
327 // Create a timer to update our status bar
330 m_timer
= new MyTimer(this);
337 //Deletes child objects implicitly and our timer explicitly
347 //Called from file->quit.
348 //Closes this application.
350 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
352 // true is to force the frame to close
359 //Called from help->about.
360 //Gets some info about this application.
362 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
365 msg
.Printf( _T("This is a test of wxMediaCtrl.\n")
366 _T("Welcome to %s"), wxVERSION_STRING
);
368 wxMessageBox(msg
, _T("About wxMediaCtrl test"), wxOK
| wxICON_INFORMATION
, this);
374 //Called from file->loop.
375 //Changes the state of whether we want to loop or not.
377 void MyFrame
::OnLoop(wxCommandEvent
& WXUNUSED(event
))
385 //Called from file->openfile.
386 //Opens and plays a media file
388 void MyFrame
::OnOpenFile(wxCommandEvent
& WXUNUSED(event
))
390 wxFileDialog
fd(this);
392 if(fd
.ShowModal() == wxID_OK
)
394 if( !m_mediactrl
->Load(fd
.GetPath()) )
395 wxMessageBox(wxT("Couldn't load file!"));
397 if( !m_mediactrl
->Play() )
398 wxMessageBox(wxT("Couldn't play movie!"));
408 //Called from file->openurl.
410 //Windows DirectShow only.
412 void MyFrame
::OnOpenURL(wxCommandEvent
& WXUNUSED(event
))
414 wxString theURL
= wxGetTextFromUser(wxT("Enter the URL that has the movie to play"));
416 #if defined(__WXMSW__) && wxUSE_DIRECTSHOW
419 if( !m_mediactrl
->Load(wxURI(theURL
)) )
420 wxMessageBox(wxT("Couldn't load URL!"));
422 if( !m_mediactrl
->Play() )
423 wxMessageBox(wxT("Couldn't play movie!"));
428 wxMessageBox(wxT("Not supported!"));
435 //Called from file->play.
436 //Resumes the media if it is paused or stopped.
438 void MyFrame
::OnPlay(wxCommandEvent
& WXUNUSED(event
))
440 if( !m_mediactrl
->Play() )
441 wxMessageBox(wxT("Couldn't play movie!"));
447 //Called from file->pause.
448 //Pauses the media in-place.
450 void MyFrame
::OnPause(wxCommandEvent
& WXUNUSED(event
))
452 if( !m_mediactrl
->Pause() )
453 wxMessageBox(wxT("Couldn't pause movie!"));
459 //Called from file->stop.
460 //Note that where the media stops is undefined -
461 //it could stop at the end or beginning.
463 void MyFrame
::OnStop(wxCommandEvent
& WXUNUSED(event
))
465 if( !m_mediactrl
->Stop() )
466 wxMessageBox(wxT("Couldn't stop movie!"));
472 //Called from file->seek.
473 //Called when the user moves the slider -
474 //seeks to a position within the media
476 void MyFrame
::OnSeek(wxCommandEvent
& WXUNUSED(event
))
478 if( !m_mediactrl
->SetPosition( m_slider
->GetValue() * 1000 ) )
479 wxMessageBox(wxT("Couldn't seek in movie!"));
485 //Called when the media stops playing.
486 //Here we loop it if the user wants to (has been selected from file menu)
488 void MyFrame
::OnMediaFinished(wxMediaEvent
& WXUNUSED(event
))
492 if ( !m_mediactrl
->SetPosition(0) || !m_mediactrl
->Play() )
493 wxMessageBox(wxT("Couldn't seek or play to loop movie!"));