]>
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 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 // the application icon (under Windows and OS/2 it is in resources)
34 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
35 #include "../sample.xpm"
38 #define WAV_FILE _T("doggrowl.wav")
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 class MyApp
: public wxApp
47 virtual bool OnInit();
51 class MyFrame
: public wxFrame
55 MyFrame(const wxString
& title
);
56 ~MyFrame() { delete m_sound
; }
58 // event handlers (these functions should _not_ be virtual)
59 void OnPlaySync(wxCommandEvent
& event
);
60 void OnPlayAsync(wxCommandEvent
& event
);
61 void OnPlayAsyncOnStack(wxCommandEvent
& event
);
62 void OnPlayLoop(wxCommandEvent
& event
);
64 void OnQuit(wxCommandEvent
& event
);
65 void OnAbout(wxCommandEvent
& event
);
70 // any class wishing to process wxWindows events must use this macro
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 // IDs for the controls and the menu commands
82 Sound_Quit
= wxID_EXIT
,
83 Sound_About
= wxID_ABOUT
,
84 Sound_PlaySync
= wxID_HIGHEST
+ 1,
86 Sound_PlayAsyncOnStack
,
90 // ----------------------------------------------------------------------------
91 // event tables and other macros for wxWindows
92 // ----------------------------------------------------------------------------
94 // the event tables connect the wxWindows events with the functions (event
95 // handlers) which process them. It can be also done at run-time, but for the
96 // simple menu events like this the static method is much simpler.
97 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
98 EVT_MENU(Sound_Quit
, MyFrame::OnQuit
)
99 EVT_MENU(Sound_About
, MyFrame::OnAbout
)
100 EVT_MENU(Sound_PlaySync
, MyFrame::OnPlaySync
)
101 EVT_MENU(Sound_PlayAsync
, MyFrame::OnPlayAsync
)
102 EVT_MENU(Sound_PlayAsyncOnStack
, MyFrame::OnPlayAsync
)
103 EVT_MENU(Sound_PlayLoop
, MyFrame::OnPlayLoop
)
106 // Create a new application object: this macro will allow wxWindows to create
107 // the application object during program execution (it's better than using a
108 // static object for many reasons) and also implements the accessor function
109 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
113 // ============================================================================
115 // ============================================================================
117 // ----------------------------------------------------------------------------
118 // the application class
119 // ----------------------------------------------------------------------------
121 // 'Main program' equivalent: the program execution "starts" here
124 // create the main application window
125 MyFrame
*frame
= new MyFrame(_T("wxWindows Sound Sample"));
127 // and show it (the frames, unlike simple controls, are not shown when
128 // created initially)
131 // success: wxApp::OnRun() will be called which will enter the main message
132 // loop and the application will run. If we returned false here, the
133 // application would exit immediately.
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
142 MyFrame::MyFrame(const wxString
& title
)
143 : wxFrame(NULL
, wxID_ANY
, title
)
147 // set the frame icon
148 SetIcon(wxICON(sample
));
150 wxMenu
*menuFile
= new wxMenu
;
151 wxMenu
*helpMenu
= new wxMenu
;
152 wxMenu
*playMenu
= new wxMenu
;
153 helpMenu
->Append(Sound_About
, _T("&About...\tF1"), _T("Show about dialog"));
154 menuFile
->Append(Sound_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
155 playMenu
->Append(Sound_PlaySync
, _T("Play sound &synchronously"));
156 playMenu
->Append(Sound_PlayAsync
, _T("Play sound &asynchronously"));
157 playMenu
->Append(Sound_PlayAsync
, _T("Play sound asynchronously (&object on stack)"));
158 playMenu
->Append(Sound_PlayLoop
, _T("&Loop sound"));
160 // now append the freshly created menu to the menu bar...
161 wxMenuBar
*menuBar
= new wxMenuBar();
162 menuBar
->Append(menuFile
, _T("&File"));
163 menuBar
->Append(playMenu
, _T("&Play"));
164 menuBar
->Append(helpMenu
, _T("&Help"));
166 // ... and attach this menu bar to the frame
173 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
175 // true is to force the frame to close
179 void MyFrame::OnPlaySync(wxCommandEvent
& WXUNUSED(event
))
183 m_sound
= new wxSound(WAV_FILE
);
185 m_sound
->Play(wxSOUND_SYNC
);
188 void MyFrame::OnPlayAsync(wxCommandEvent
& WXUNUSED(event
))
192 m_sound
= new wxSound(WAV_FILE
);
194 m_sound
->Play(wxSOUND_ASYNC
);
197 void MyFrame::OnPlayAsyncOnStack(wxCommandEvent
& WXUNUSED(event
))
200 wxSound
snd(WAV_FILE
);
202 snd
.Play(wxSOUND_ASYNC
);
205 void MyFrame::OnPlayLoop(wxCommandEvent
& WXUNUSED(event
))
209 m_sound
= new wxSound(WAV_FILE
);
211 m_sound
->Play(wxSOUND_ASYNC
| wxSOUND_LOOP
);
214 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
217 msg
.Printf( _T("This is the About dialog of the sound sample.\n")
218 _T("Welcome to %s"), wxVERSION_STRING
);
220 wxMessageBox(msg
, _T("About"), wxOK
| wxICON_INFORMATION
, this);