]>
git.saurik.com Git - wxWidgets.git/blob - samples/sound/sound.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Example of sound playing in wxWindows
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" wxWindows headers)
24 #include "wx/msgdlg.h"
29 #include "wx/numdlg.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // the application icon (under Windows and OS/2 it is in resources)
36 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
37 #include "../sample.xpm"
40 #define WAV_FILE _T("doggrowl.wav")
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 class MyApp
: public wxApp
49 virtual bool OnInit();
53 class MyFrame
: public wxFrame
57 MyFrame(const wxString
& title
);
58 ~MyFrame() { delete m_sound
; }
60 // event handlers (these functions should _not_ be virtual)
61 void OnPlaySync(wxCommandEvent
& event
);
62 void OnPlayAsync(wxCommandEvent
& event
);
63 void OnPlayAsyncOnStack(wxCommandEvent
& event
);
64 void OnPlayLoop(wxCommandEvent
& event
);
66 void OnQuit(wxCommandEvent
& event
);
67 void OnAbout(wxCommandEvent
& event
);
72 // any class wishing to process wxWindows events must use this macro
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // IDs for the controls and the menu commands
84 Sound_Quit
= wxID_EXIT
,
85 Sound_About
= wxID_ABOUT
,
86 Sound_PlaySync
= wxID_HIGHEST
+ 1,
88 Sound_PlayAsyncOnStack
,
92 // ----------------------------------------------------------------------------
93 // event tables and other macros for wxWindows
94 // ----------------------------------------------------------------------------
96 // the event tables connect the wxWindows events with the functions (event
97 // handlers) which process them. It can be also done at run-time, but for the
98 // simple menu events like this the static method is much simpler.
99 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
100 EVT_MENU(Sound_Quit
, MyFrame::OnQuit
)
101 EVT_MENU(Sound_About
, MyFrame::OnAbout
)
102 EVT_MENU(Sound_PlaySync
, MyFrame::OnPlaySync
)
103 EVT_MENU(Sound_PlayAsync
, MyFrame::OnPlayAsync
)
104 EVT_MENU(Sound_PlayAsyncOnStack
, MyFrame::OnPlayAsync
)
105 EVT_MENU(Sound_PlayLoop
, MyFrame::OnPlayLoop
)
108 // Create a new application object: this macro will allow wxWindows to create
109 // the application object during program execution (it's better than using a
110 // static object for many reasons) and also implements the accessor function
111 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
120 // the application class
121 // ----------------------------------------------------------------------------
123 // 'Main program' equivalent: the program execution "starts" here
126 // create the main application window
127 MyFrame
*frame
= new MyFrame(_T("wxWindows Sound Sample"));
129 // and show it (the frames, unlike simple controls, are not shown when
130 // created initially)
133 // success: wxApp::OnRun() will be called which will enter the main message
134 // loop and the application will run. If we returned false here, the
135 // application would exit immediately.
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
144 MyFrame::MyFrame(const wxString
& title
)
145 : wxFrame(NULL
, wxID_ANY
, title
)
149 // set the frame icon
150 SetIcon(wxICON(sample
));
152 wxMenu
*menuFile
= new wxMenu
;
153 wxMenu
*helpMenu
= new wxMenu
;
154 wxMenu
*playMenu
= new wxMenu
;
155 helpMenu
->Append(Sound_About
, _T("&About...\tF1"), _T("Show about dialog"));
156 menuFile
->Append(Sound_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
157 playMenu
->Append(Sound_PlaySync
, _T("Play sound &synchronously"));
158 playMenu
->Append(Sound_PlayAsync
, _T("Play sound &asynchronously"));
159 playMenu
->Append(Sound_PlayAsync
, _T("Play sound asynchronously (&object on stack)"));
160 playMenu
->Append(Sound_PlayLoop
, _T("&Loop sound"));
162 // now append the freshly created menu to the menu bar...
163 wxMenuBar
*menuBar
= new wxMenuBar();
164 menuBar
->Append(menuFile
, _T("&File"));
165 menuBar
->Append(playMenu
, _T("&Play"));
166 menuBar
->Append(helpMenu
, _T("&Help"));
168 // ... and attach this menu bar to the frame
175 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
177 // true is to force the frame to close
181 void MyFrame::OnPlaySync(wxCommandEvent
& WXUNUSED(event
))
185 m_sound
= new wxSound(WAV_FILE
);
187 m_sound
->Play(wxSOUND_SYNC
);
190 void MyFrame::OnPlayAsync(wxCommandEvent
& WXUNUSED(event
))
194 m_sound
= new wxSound(WAV_FILE
);
196 m_sound
->Play(wxSOUND_ASYNC
);
199 void MyFrame::OnPlayAsyncOnStack(wxCommandEvent
& WXUNUSED(event
))
202 wxSound
snd(WAV_FILE
);
204 snd
.Play(wxSOUND_ASYNC
);
207 void MyFrame::OnPlayLoop(wxCommandEvent
& WXUNUSED(event
))
211 m_sound
= new wxSound(WAV_FILE
);
213 m_sound
->Play(wxSOUND_ASYNC
| wxSOUND_LOOP
);
216 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
219 msg
.Printf( _T("This is the About dialog of the sound sample.\n")
220 _T("Welcome to %s"), wxVERSION_STRING
);
222 wxMessageBox(msg
, _T("About"), wxOK
| wxICON_INFORMATION
, this);