Playback sound and music

Initialize ClassESound and load Sound effects and Music

Using ClassESound is easy, only few lines of code are needed to start playing sound and music. Minimizing and other events are automatically handled by EDGELIB. Open ClassESound on program initialization. Add the OnInit() event to the framework:

helloworld.cpp (5 lines)
  1.     public:
  2.         ClassMain(void);
  3.         ~ClassMain(void);
  4.         ERESULT OnInit(ENATIVETYPE instance);
  5.         ERESULT OnNextFrame(ClassEDisplay *display, unsigned long timedelta);

And write the implementation:

helloworld.cpp (14 lines)
  1. //Callback: Called before the display mode changes
  2. ERESULT ClassMain::OnInit(ENATIVETYPE instance)
  3. {
  4.     if (ecd.snd->Open() == E_OK)
  5.     {
  6.         //Ready for sound playback
  7.     }
  8.     else
  9.     {
  10.         SetErrorMsg("Error opening sound output");
  11.         return(E_ERROR);
  12.     }
  13.     return(E_OK);
  14. }

When opening the sound system, it takes no sound parameters. Settings are picked automatically to match the target platform. An additional overloaded function is provided to change these default settings. ClassESound is closed automatically on application shutdown.

Add the following code to load music and a sound effect:

helloworld.cpp (15 lines)
  1. //Callback: Called before the display mode changes
  2. ERESULT ClassMain::OnInit(ENATIVETYPE instance)
  3. {
  4.     if (ecd.snd->Open() == E_OK)
  5.     {
  6.         ecd.snd->LoadSoundEffect(0, "sound.wav");
  7.         ecd.snd->LoadMusic(0, "music.mod");
  8.     }
  9.     else
  10.     {
  11.         SetErrorMsg("Error opening sound output");
  12.         return(E_ERROR);
  13.     }
  14.     return(E_OK);
  15. }

Playing sound and music

First play some music after it has been loaded. Modify the OnInit() event:

helloworld.cpp (6 lines)
  1.     if (ecd.snd->Open())
  2.     {
  3.         ecd.snd->LoadSoundEffect(0, "sound.wav");
  4.         if (ecd.snd->LoadMusic(0, "music.mod") == E_OK)
  5.             ecd.snd->PlayMusic(0, true);
  6.     }

Then modify the OnButtonDown() event to play the sound effect when button A is pressed:

helloworld.cpp (8 lines)
  1. //Callback: Called when the user pressed a key or button
  2. void ClassMain::OnButtonDown(unsigned long bnr, EBUTTONLIST *blist)
  3. {
  4.     if (bnr == blist->ButtonA)
  5.         ecd.snd->PlaySound(0);
  6.     else
  7.         Quit();
  8. }

Finally modify OnNextFrame() to display additional information about the program:

helloworld.cpp (8 lines)
  1. //Callback: Called every frame
  2. ERESULT ClassMain::OnNextFrame(ClassEDisplay *display, unsigned long timedelta)
  3. {
  4.     display->buffer.DrawFont(0, 0, &display->fontinternal, "Opened ClassESound...\nUsing Hekkus wrapper");
  5.     display->buffer.DrawFont(0, display->fontinternal.GetHeight() * 3, &display->fontinternal, "A: Play sound effect");
  6.     display->buffer.DrawFont(0, display->fontinternal.GetHeight() * 4, &display->fontinternal, "Any other key: Quit");
  7.     return(E_OK);
  8. }

Download tutorial project

Here is a sample that can be downloaded which contains the result of this tutorial.

Chapters

Latest forum posts