My SDL_Mixer class is not playing sound, but compiles

Hey guys. I am writing a very simple class to handle audio for a game I’m working with. I was able to get the code to compile but it will not play the sound. My error checking shows that I am initializing correctly, loading the file correctly and should be playing the sound. Yes my sound is not muted, I am not stupid.

here is my header file

Code:
#include “SDL_mixer.h”

//SDL_mixer class
//This code was written by Joseph Yaeger 10/04/2009
//
//In oder to use this class you must follow these directions
//
//1) create object of the the class
//2) use the init function to start up the SDL_mixer
//3) use the loadAudio() function to load the sounds into the variables
//4) now you can call any of the sounds, you must pass in a int which will represent
// the number of times the sound will be played
//5) use the cleanUp() function to release memory and close SDL_mixer
//
class audio
{
public:
audio();
bool init();
bool loadAudio();
bool mediumButton(int);
void cleanUp();
private:
Mix_Chunk *buttonMedium;

};

Here is my cpp file for my audio class

Code:
#include “Audio.h”
#include “SDL_mixer.h”

///////////////////////////////////////////////////////////////////////////////////////////////////////////
audio::audio()
{
//no comment
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////

/*********************************************************************************************************/
//sets up SDL_mixer, may also load files later.
bool audio::init()
{
//initializes SDL_mixer and also checks to make sure it was initialized correctly
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
{
//returns false if there was a problem initializing SDL_mixer
return false;
}

//returns true if there were no problems initializing SDL_mixer
return true;

}//end of init
/********************************************************************************************************/

/////////////////////////////////////////////////////////////////////////////////////////////////////////
bool audio::loadAudio()
{
//Loads audio into the correct data to be used later
buttonMedium = Mix_LoadWAV(“medium.wav”);

//checks to make sure that the effect was loaded correctly
if (buttonMedium == NULL)
{
	//returns false if there was a problem
	return false;
}

//return
return true;

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////

/********************************************************************************************************/
bool audio::mediumButton( int plays)
{
//plays the sound file that is passed in. plays indicated number of times the sound will play.
if( Mix_PlayChannel(-1, buttonMedium, plays)== -1)
{
return false;
}

return true;

}
/*********************************************************************************************************/

///////////////////////////////////////////////////////////////////////////////////////////////////////////
void audio::cleanUp()
{
//releases the memory used to hold the audio information
Mix_FreeChunk(buttonMedium);

//Mix_FreeMusic()

//Closes SDL_mixer
Mix_CloseAudio();

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////

and here is my simple main

Code:
#include “SDL.h”
#include “SDL_mixer.h”
#include “Audio.h”

int main(int argc, char* argv[])
{
bool check,check2,check3;

//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );

audio sound;

check = sound.init();

check2 = sound.loadAudio();

check3 = sound.mediumButton(5);

sound.cleanUp();

//Quit SDL
SDL_Quit();

return 0;

}

I’ve no idea what my problem is. This is the first time I have tried using SDL_mixer. If any of you can help me out I would greatly appreciate it. Thanks in advanced.

What platform are you running on? What version of SDL are you using? What
version of SDL_mixer are you using?

Do the SDL_mixer test programs playwave and playmus work for you?On Mon, Oct 5, 2009 at 3:01 PM, Call Me Metal <raiko_the_sightless at yahoo.com wrote:

Hey guys. I am writing a very simple class to handle audio for a game I’m
working with. I was able to get the code to compile but it will not play the
sound. My error checking shows that I am initializing correctly, loading the
file correctly and should be playing the sound. Yes my sound is not muted, I
am not stupid.

here is my header file

Code:

#include “SDL_mixer.h”

//SDL_mixer class
//This code was written by Joseph Yaeger 10/04/2009
//
//In oder to use this class you must follow these directions
//
//1) create object of the the class
//2) use the init function to start up the SDL_mixer
//3) use the loadAudio() function to load the sounds into the variables
//4) now you can call any of the sounds, you must pass in a int which will
represent
// the number of times the sound will be played
//5) use the cleanUp() function to release memory and close SDL_mixer
//
class audio
{
public:
audio();
bool init();
bool loadAudio();
bool mediumButton(int);
void cleanUp();
private:
Mix_Chunk *buttonMedium;

};

Here is my cpp file for my audio class

Code:

#include “Audio.h”
#include “SDL_mixer.h”

///////////////////////////////////////////////////////////////////////////////////////////////////////////
audio::audio()
{
//no comment
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

/*********************************************************************************************************/
//sets up SDL_mixer, may also load files later.
bool audio::init()
{
//initializes SDL_mixer and also checks to make sure it was initialized
correctly
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
{
//returns false if there was a problem initializing SDL_mixer
return false;
}

//returns true if there were no problems initializing SDL_mixer
return true;
}//end of init

/********************************************************************************************************/

/////////////////////////////////////////////////////////////////////////////////////////////////////////
bool audio::loadAudio()
{
//Loads audio into the correct data to be used later
buttonMedium = Mix_LoadWAV(“medium.wav”);

//checks to make sure that the effect was loaded correctly
if (buttonMedium == NULL)
{
//returns false if there was a problem
return false;
}

//return
return true;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

/********************************************************************************************************/
bool audio::mediumButton( int plays)
{
//plays the sound file that is passed in. plays indicated number of
times the sound will play.
if( Mix_PlayChannel(-1, buttonMedium, plays)== -1)
{
return false;
}

return true;
}

/*********************************************************************************************************/

///////////////////////////////////////////////////////////////////////////////////////////////////////////
void audio::cleanUp()
{
//releases the memory used to hold the audio information
Mix_FreeChunk(buttonMedium);

//Mix_FreeMusic()

//Closes SDL_mixer
Mix_CloseAudio();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

and here is my simple main

Code:

#include “SDL.h”
#include “SDL_mixer.h”
#include “Audio.h”

int main(int argc, char* argv[])
{
bool check,check2,check3;

//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );

audio sound;

check = sound.init();

check2 = sound.loadAudio();

check3 = sound.mediumButton(5);

sound.cleanUp();

//Quit SDL
SDL_Quit();

return 0;
}

I’ve no idea what my problem is. This is the first time I have tried using
SDL_mixer. If any of you can help me out I would greatly appreciate it.
Thanks in advanced.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


-Sam Lantinga, Founder and President, Galaxy Gameworks LLC