1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mediaplayer.cpp
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 (unsigned)(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
);
151 m_frame
->SetStatusText(wxString::Format(_T("%s Pos:%u State:%s"),
152 m_frame
->m_basestatus
.c_str(),
153 (unsigned int)lPosition
,
154 wxGetMediaStateText(m_frame
->m_mediactrl
->GetState())
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 // IDs for the controls and the menu commands
171 Minimal_Quit
= wxID_EXIT
,
177 Minimal_About
= wxID_ABOUT
,
182 // id for our wxMediaCtrl
186 // ----------------------------------------------------------------------------
187 // event tables and other macros for wxWidgets
188 // ----------------------------------------------------------------------------
190 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
192 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
193 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
194 EVT_MENU(Minimal_Loop
, MyFrame::OnLoop
)
195 EVT_MENU(Minimal_OpenFile
, MyFrame::OnOpenFile
)
196 EVT_MENU(Minimal_Play
, MyFrame::OnPlay
)
197 EVT_MENU(Minimal_Pause
, MyFrame::OnPause
)
198 EVT_MENU(Minimal_Stop
, MyFrame::OnStop
)
201 EVT_SLIDER(Minimal_Slider
, MyFrame::OnSeek
)
204 EVT_MEDIA_FINISHED(Minimal_Media
, MyFrame::OnMediaFinished
)
210 // ============================================================================
212 // ============================================================================
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 // 'Main program' equivalent: the program execution "starts" here
221 MyFrame
*frame
= new MyFrame(_T("Minimal wxWidgets App"));
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
234 //Creates our menus and controls
236 MyFrame::MyFrame(const wxString
& title
)
237 : wxFrame(NULL
, wxID_ANY
, title
), m_timer(NULL
)
243 wxMenu
*menuFile
= new wxMenu
;
245 wxMenu
*helpMenu
= new wxMenu
;
246 helpMenu
->Append(Minimal_About
, _T("&About...\tF1"), _T("Show about dialog"));
248 menuFile
->Append(Minimal_OpenFile
, _T("&Open File"), _T("Open a File"));
249 menuFile
->AppendSeparator();
250 menuFile
->Append(Minimal_Play
, _T("&Play"), _T("Resume playback"));
251 menuFile
->Append(Minimal_Pause
, _T("P&ause"), _T("Pause playback"));
252 menuFile
->Append(Minimal_Stop
, _T("&Stop"), _T("Stop playback"));
253 menuFile
->AppendSeparator();
254 menuFile
->AppendCheckItem(Minimal_Loop
, _T("&Loop"), _T("Loop Selected Media"));
255 menuFile
->AppendSeparator();
256 menuFile
->Append(Minimal_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
258 wxMenuBar
*menuBar
= new wxMenuBar();
259 menuBar
->Append(menuFile
, _T("&File"));
260 menuBar
->Append(helpMenu
, _T("&Help"));
263 #endif // wxUSE_MENUS
266 // Create and attach the first/main sizer
269 m_sizer
= new wxBoxSizer(wxVERTICAL
);
270 this->SetSizer(m_sizer
);
271 this->SetAutoLayout(true);
274 // Create our media control
277 m_mediactrl
= new wxMediaCtrl(this, Minimal_Media
, wxT(""));
278 m_sizer
->Add(m_mediactrl
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
284 m_slider
= new wxSlider(this, Minimal_Slider
, 0, //init
287 wxDefaultPosition
, wxDefaultSize
,
289 m_sizer
->Add(m_slider
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
|wxEXPAND
, 5);
293 // Create the second sizer which will position things
296 // -------Menu----------
302 wxBoxSizer
* horzsizer
= new wxBoxSizer(wxHORIZONTAL
);
303 m_sizer
->Add(horzsizer
, 0, wxALIGN_CENTER_HORIZONTAL
|wxALL
, 5);
306 // We arn't looping initially
312 // Create our status bar
315 // create a status bar just for fun (by default with 1 pane only)
318 SetStatusText(m_basestatus
);
319 #endif // wxUSE_STATUSBAR
322 // Create a timer to update our status bar
325 m_timer
= new MyTimer(this);
332 //Deletes child objects implicitly and our timer explicitly
342 //Called from file->quit.
343 //Closes this application.
345 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
347 // true is to force the frame to close
354 //Called from help->about.
355 //Gets some info about this application.
357 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
360 msg
.Printf( _T("This is a test of wxMediaCtrl.\n")
361 _T("Welcome to %s"), wxVERSION_STRING
);
363 wxMessageBox(msg
, _T("About wxMediaCtrl test"), wxOK
| wxICON_INFORMATION
, this);
369 //Called from file->loop.
370 //Changes the state of whether we want to loop or not.
372 void MyFrame::OnLoop(wxCommandEvent
& WXUNUSED(event
))
380 //Called from file->openfile.
381 //Opens and plays a media file
383 void MyFrame::OnOpenFile(wxCommandEvent
& WXUNUSED(event
))
385 wxFileDialog
fd(this);
387 if(fd
.ShowModal() == wxID_OK
)
389 if( !m_mediactrl
->Load(fd
.GetPath()) )
390 wxMessageBox(wxT("Couldn't load file!"));
392 if( !m_mediactrl
->Play() )
393 wxMessageBox(wxT("Couldn't play movie!"));
402 //Called from file->play.
403 //Resumes the media if it is paused or stopped.
405 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
407 if( !m_mediactrl
->Play() )
408 wxMessageBox(wxT("Couldn't play movie!"));
414 //Called from file->pause.
415 //Pauses the media in-place.
417 void MyFrame::OnPause(wxCommandEvent
& WXUNUSED(event
))
419 if( !m_mediactrl
->Pause() )
420 wxMessageBox(wxT("Couldn't pause movie!"));
426 //Called from file->stop.
427 //Where it stops depends on whether you can seek in the
428 //media control or not - if you can it stops and seeks to the beginning,
429 //otherwise it will appear to be at the end - but it will start over again
430 //when play() is called
432 void MyFrame::OnStop(wxCommandEvent
& WXUNUSED(event
))
434 if( !m_mediactrl
->Stop() )
435 wxMessageBox(wxT("Couldn't stop movie!"));
441 //Called from file->seek.
442 //Called when the user moves the slider -
443 //seeks to a position within the media
445 void MyFrame::OnSeek(wxCommandEvent
& WXUNUSED(event
))
447 if( !m_mediactrl
->SetPosition( m_slider
->GetValue() * 1000 ) )
448 wxMessageBox(wxT("Couldn't seek in movie!"));
454 //Called when the media stops playing.
455 //Here we loop it if the user wants to (has been selected from file menu)
457 void MyFrame::OnMediaFinished(wxMediaEvent
& WXUNUSED(event
))
461 if ( !m_mediactrl
->Play() )
462 wxMessageBox(wxT("Couldn't loop movie!"));