]> git.saurik.com Git - wxWidgets.git/blob - samples/sound/sound.cpp
added Stop test
[wxWidgets.git] / samples / sound / sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sound.cpp
3 // Purpose: Example of sound playing in wxWidgets
4 // Author: Vaclav Slavik
5 // Modified by:
6 // Created: 2004/01/29
7 // RCS-ID: $Id$
8 // Copyright: (c) 2004 Vaclav Salvik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 // for all others, include the necessary headers (this file is usually all you
19 // need because it includes almost all "standard" wxWidgets headers)
20 #ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #include "wx/frame.h"
23 #include "wx/menu.h"
24 #include "wx/msgdlg.h"
25 #include "wx/icon.h"
26 #include "wx/textctrl.h"
27 #include "wx/filedlg.h"
28 #endif
29
30 #include "wx/sound.h"
31 #include "wx/numdlg.h"
32 #include "wx/textdlg.h"
33
34 // ----------------------------------------------------------------------------
35 // resources
36 // ----------------------------------------------------------------------------
37
38 // the application icon (under Windows and OS/2 it is in resources)
39 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
40 #include "../sample.xpm"
41 #endif
42
43 #define WAV_FILE _T("doggrowl.wav")
44
45 // ----------------------------------------------------------------------------
46 // private classes
47 // ----------------------------------------------------------------------------
48
49 class MyApp : public wxApp
50 {
51 public:
52 virtual bool OnInit();
53 };
54
55
56 class MyFrame : public wxFrame
57 {
58 public:
59 // ctor(s)
60 MyFrame(const wxString& title);
61 ~MyFrame() { delete m_sound; }
62
63 // event handlers (these functions should _not_ be virtual)
64 void OnSelectFile(wxCommandEvent& event);
65 #ifdef __WXMSW__
66 void OnSelectResource(wxCommandEvent& event);
67 #endif // __WXMSW__
68 void OnQuit(wxCommandEvent& event);
69
70 void OnPlaySync(wxCommandEvent& event);
71 void OnPlayAsync(wxCommandEvent& event);
72 void OnPlayAsyncOnStack(wxCommandEvent& event);
73 void OnPlayLoop(wxCommandEvent& event);
74 void OnStop(wxCommandEvent& event);
75
76 void OnAbout(wxCommandEvent& event);
77
78 void NotifyUsingFile(const wxString& name);
79
80
81 private:
82 wxSound *CreateSound() const;
83
84 wxSound* m_sound;
85 wxString m_soundFile;
86 #ifdef __WXMSW__
87 wxString m_soundRes;
88 #endif // __WXMSW__
89 wxTextCtrl* m_tc;
90
91 // any class wishing to process wxWidgets events must use this macro
92 DECLARE_EVENT_TABLE()
93 };
94
95 // ----------------------------------------------------------------------------
96 // constants
97 // ----------------------------------------------------------------------------
98
99 // IDs for the controls and the menu commands
100 enum
101 {
102 // menu items
103 Sound_SelectFile = wxID_HIGHEST + 1,
104 #ifdef __WXMSW__
105 Sound_SelectResource,
106 #endif // __WXMSW__
107
108 Sound_PlaySync,
109 Sound_PlayAsync,
110 Sound_PlayAsyncOnStack,
111 Sound_PlayLoop,
112 Sound_Stop,
113
114 Sound_Quit = wxID_EXIT,
115 Sound_About = wxID_ABOUT
116 };
117
118 // ----------------------------------------------------------------------------
119 // event tables and other macros for wxWidgets
120 // ----------------------------------------------------------------------------
121
122 // the event tables connect the wxWidgets events with the functions (event
123 // handlers) which process them. It can be also done at run-time, but for the
124 // simple menu events like this the static method is much simpler.
125 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
126 EVT_MENU(Sound_SelectFile, MyFrame::OnSelectFile)
127 #ifdef __WXMSW__
128 EVT_MENU(Sound_SelectResource, MyFrame::OnSelectResource)
129 #endif // __WXMSW__
130 EVT_MENU(Sound_Quit, MyFrame::OnQuit)
131 EVT_MENU(Sound_About, MyFrame::OnAbout)
132 EVT_MENU(Sound_PlaySync, MyFrame::OnPlaySync)
133 EVT_MENU(Sound_PlayAsync, MyFrame::OnPlayAsync)
134 EVT_MENU(Sound_PlayAsyncOnStack, MyFrame::OnPlayAsyncOnStack)
135 EVT_MENU(Sound_PlayLoop, MyFrame::OnPlayLoop)
136 EVT_MENU(Sound_Stop, MyFrame::OnStop)
137 END_EVENT_TABLE()
138
139 // Create a new application object: this macro will allow wxWidgets to create
140 // the application object during program execution (it's better than using a
141 // static object for many reasons) and also implements the accessor function
142 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
143 // not wxApp)
144 IMPLEMENT_APP(MyApp)
145
146 // ============================================================================
147 // implementation
148 // ============================================================================
149
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
153
154 // 'Main program' equivalent: the program execution "starts" here
155 bool MyApp::OnInit()
156 {
157 // create the main application window
158 MyFrame *frame = new MyFrame(_T("wxWidgets Sound Sample"));
159
160 // and show it (the frames, unlike simple controls, are not shown when
161 // created initially)
162 frame->Show(true);
163
164 // success: wxApp::OnRun() will be called which will enter the main message
165 // loop and the application will run. If we returned false here, the
166 // application would exit immediately.
167 return true;
168 }
169
170 // ----------------------------------------------------------------------------
171 // main frame
172 // ----------------------------------------------------------------------------
173
174 // frame constructor
175 MyFrame::MyFrame(const wxString& title)
176 : wxFrame(NULL, wxID_ANY, title)
177 {
178 m_sound = NULL;
179 m_soundFile = WAV_FILE;
180
181 // set the frame icon
182 SetIcon(wxICON(sample));
183
184 wxMenu *menuFile = new wxMenu;
185 wxMenu *helpMenu = new wxMenu;
186 wxMenu *playMenu = new wxMenu;
187 helpMenu->Append(Sound_About, _T("&About...\tF1"), _T("Show about dialog"));
188 menuFile->Append(Sound_SelectFile, _T("Select WAV &file\tCtrl+O"), _T("Select a new wav file to play"));
189 #ifdef __WXMSW__
190 menuFile->Append(Sound_SelectResource, _T("Select WAV &resource\tCtrl+R"), _T("Select a new resource to play"));
191 #endif // __WXMSW__
192 menuFile->Append(Sound_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
193 playMenu->Append(Sound_PlaySync, _T("Play sound &synchronously\tCtrl+S"));
194 playMenu->Append(Sound_PlayAsync, _T("Play sound &asynchronously\tCtrl+A"));
195 playMenu->Append(Sound_PlayAsyncOnStack, _T("Play sound asynchronously (&object on stack)\tCtrl+T"));
196 playMenu->Append(Sound_PlayLoop, _T("&Loop sound\tCtrl+L"));
197 playMenu->AppendSeparator();
198 playMenu->Append(Sound_Stop, _T("&Stop playing\tCtrl-C"));
199
200 // now append the freshly created menu to the menu bar...
201 wxMenuBar *menuBar = new wxMenuBar();
202 menuBar->Append(menuFile, _T("&File"));
203 menuBar->Append(playMenu, _T("&Play"));
204 menuBar->Append(helpMenu, _T("&Help"));
205
206 // ... and attach this menu bar to the frame
207 SetMenuBar(menuBar);
208
209 m_tc = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
210 wxDefaultPosition, wxDefaultSize,
211 wxTE_MULTILINE|wxTE_READONLY);
212 NotifyUsingFile(m_soundFile);
213 }
214
215
216 wxSound *MyFrame::CreateSound() const
217 {
218 #ifdef __WXMSW__
219 if ( !m_soundRes.empty() )
220 {
221 return new wxSound(m_soundRes, true);
222 }
223 #endif // __WXMSW__
224
225 return new wxSound(m_soundFile);
226 }
227
228
229 void MyFrame::NotifyUsingFile(const wxString& name)
230 {
231 wxString msg;
232 msg << _T("Using sound file: ") << name << _T("\n");
233 m_tc->AppendText(msg);
234 }
235
236
237 // event handlers
238
239
240 void MyFrame::OnSelectFile(wxCommandEvent& WXUNUSED(event))
241 {
242 wxFileDialog dlg(this, _T("Choose a sound file"),
243 wxEmptyString, wxEmptyString,
244 _T("WAV files (*.wav)|*.wav"), wxOPEN|wxCHANGE_DIR);
245 if ( dlg.ShowModal() == wxID_OK )
246 {
247 m_soundFile = dlg.GetPath();
248 #ifdef __WXMSW__
249 m_soundRes.clear();
250 #endif // __WXMSW__
251
252 delete m_sound;
253 m_sound = NULL;
254 NotifyUsingFile(m_soundFile);
255 }
256 }
257
258 #ifdef __WXMSW__
259
260 void MyFrame::OnSelectResource(wxCommandEvent& WXUNUSED(event))
261 {
262 m_soundRes = wxGetTextFromUser
263 (
264 _T("Enter resource name:"),
265 _T("wxWidgets Sound Sample"),
266 _T("FromResource"),
267 this
268 );
269 if ( m_soundRes.empty() )
270 return;
271
272 m_soundFile.clear();
273 delete m_sound;
274 m_sound = NULL;
275
276 NotifyUsingFile(_T("Windows WAV resource"));
277 }
278
279 #endif // __WXMSW__
280
281 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
282 {
283 // true is to force the frame to close
284 Close(true);
285 }
286
287 void MyFrame::OnPlaySync(wxCommandEvent& WXUNUSED(event))
288 {
289 wxBusyCursor busy;
290 if (!m_sound)
291 m_sound = CreateSound();
292 if (m_sound->IsOk())
293 m_sound->Play(wxSOUND_SYNC);
294 }
295
296 void MyFrame::OnPlayAsync(wxCommandEvent& WXUNUSED(event))
297 {
298 wxBusyCursor busy;
299 if (!m_sound)
300 m_sound = CreateSound();
301 if (m_sound->IsOk())
302 m_sound->Play(wxSOUND_ASYNC);
303 }
304
305 void MyFrame::OnPlayAsyncOnStack(wxCommandEvent& WXUNUSED(event))
306 {
307 wxBusyCursor busy;
308 wxSound snd(m_soundFile);
309 if (snd.IsOk())
310 snd.Play(wxSOUND_ASYNC);
311 }
312
313 void MyFrame::OnPlayLoop(wxCommandEvent& WXUNUSED(event))
314 {
315 wxBusyCursor busy;
316 if (!m_sound)
317 m_sound = CreateSound();
318 if (m_sound->IsOk())
319 m_sound->Play(wxSOUND_ASYNC | wxSOUND_LOOP);
320 }
321
322 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
323 {
324 wxString msg;
325 msg.Printf( _T("This is the About dialog of the sound sample.\n")
326 _T("Welcome to %s"), wxVERSION_STRING);
327
328 wxMessageBox(msg, _T("About"), wxOK | wxICON_INFORMATION, this);
329 }
330
331 void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
332 {
333 wxSound::Stop();
334 }