]>
git.saurik.com Git - wxWidgets.git/blob - samples/sound/sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Example of sound playing in wxWidgets
4 // Author: Vaclav Slavik
8 // Copyright: (c) 2004 Vaclav Salvik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 // for all others, include the necessary headers (this file is usually all you
19 // need because it includes almost all "standard" wxWidgets headers)
24 #include "wx/msgdlg.h"
26 #include "wx/textctrl.h"
27 #include "wx/filedlg.h"
31 #include "wx/numdlg.h"
32 #include "wx/textdlg.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
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"
43 #define WAV_FILE _T("doggrowl.wav")
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 class MyApp
: public wxApp
52 virtual bool OnInit();
56 class MyFrame
: public wxFrame
60 MyFrame(const wxString
& title
);
61 ~MyFrame() { delete m_sound
; }
63 // event handlers (these functions should _not_ be virtual)
64 void OnSelectFile(wxCommandEvent
& event
);
66 void OnSelectResource(wxCommandEvent
& event
);
68 void OnQuit(wxCommandEvent
& event
);
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
);
76 void OnAbout(wxCommandEvent
& event
);
78 void NotifyUsingFile(const wxString
& name
);
82 wxSound
*CreateSound() const;
91 // any class wishing to process wxWidgets events must use this macro
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // IDs for the controls and the menu commands
103 Sound_SelectFile
= wxID_HIGHEST
+ 1,
105 Sound_SelectResource
,
110 Sound_PlayAsyncOnStack
,
114 Sound_Quit
= wxID_EXIT
,
115 Sound_About
= wxID_ABOUT
118 // ----------------------------------------------------------------------------
119 // event tables and other macros for wxWidgets
120 // ----------------------------------------------------------------------------
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
)
128 EVT_MENU(Sound_SelectResource
, MyFrame::OnSelectResource
)
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
)
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
146 // ============================================================================
148 // ============================================================================
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
154 // 'Main program' equivalent: the program execution "starts" here
157 // create the main application window
158 MyFrame
*frame
= new MyFrame(_T("wxWidgets Sound Sample"));
160 // and show it (the frames, unlike simple controls, are not shown when
161 // created initially)
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.
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
175 MyFrame::MyFrame(const wxString
& title
)
176 : wxFrame(NULL
, wxID_ANY
, title
)
179 m_soundFile
= WAV_FILE
;
181 // set the frame icon
182 SetIcon(wxICON(sample
));
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"));
190 menuFile
->Append(Sound_SelectResource
, _T("Select WAV &resource\tCtrl+R"), _T("Select a new resource to play"));
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"));
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"));
206 // ... and attach this menu bar to the frame
209 m_tc
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
210 wxDefaultPosition
, wxDefaultSize
,
211 wxTE_MULTILINE
|wxTE_READONLY
);
212 NotifyUsingFile(m_soundFile
);
216 wxSound
*MyFrame::CreateSound() const
219 if ( !m_soundRes
.empty() )
221 return new wxSound(m_soundRes
, true);
225 return new wxSound(m_soundFile
);
229 void MyFrame::NotifyUsingFile(const wxString
& name
)
232 msg
<< _T("Using sound file: ") << name
<< _T("\n");
233 m_tc
->AppendText(msg
);
240 void MyFrame::OnSelectFile(wxCommandEvent
& WXUNUSED(event
))
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
)
247 m_soundFile
= dlg
.GetPath();
254 NotifyUsingFile(m_soundFile
);
260 void MyFrame::OnSelectResource(wxCommandEvent
& WXUNUSED(event
))
262 m_soundRes
= wxGetTextFromUser
264 _T("Enter resource name:"),
265 _T("wxWidgets Sound Sample"),
269 if ( m_soundRes
.empty() )
276 NotifyUsingFile(_T("Windows WAV resource"));
281 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
283 // true is to force the frame to close
287 void MyFrame::OnPlaySync(wxCommandEvent
& WXUNUSED(event
))
291 m_sound
= CreateSound();
293 m_sound
->Play(wxSOUND_SYNC
);
296 void MyFrame::OnPlayAsync(wxCommandEvent
& WXUNUSED(event
))
300 m_sound
= CreateSound();
302 m_sound
->Play(wxSOUND_ASYNC
);
305 void MyFrame::OnPlayAsyncOnStack(wxCommandEvent
& WXUNUSED(event
))
308 wxSound
snd(m_soundFile
);
310 snd
.Play(wxSOUND_ASYNC
);
313 void MyFrame::OnPlayLoop(wxCommandEvent
& WXUNUSED(event
))
317 m_sound
= CreateSound();
319 m_sound
->Play(wxSOUND_ASYNC
| wxSOUND_LOOP
);
322 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
325 msg
.Printf( _T("This is the About dialog of the sound sample.\n")
326 _T("Welcome to %s"), wxVERSION_STRING
);
328 wxMessageBox(msg
, _T("About"), wxOK
| wxICON_INFORMATION
, this);
331 void MyFrame::OnStop(wxCommandEvent
& WXUNUSED(event
))