I'll show the code:
Code: Select all
/******************************************************
*Sound Service Provider: Public Service to the People*
******************************************************/
class SoundService{
public:
SoundService();
~SoundService();
Mix_Chunk* loadSound( std::string filename );
void playSound( Mix_Chunk* sound );
void loadMusic( std::string filename );
void playMusic();
void pauseMusic();
void soundKill();
private:
bool iMusic;
bool iSound;
unsigned long int lastRew;
Mix_Music* music;
};
SoundService::SoundService(){
int audio_rate = 44100;
Uint16 audio_format = AUDIO_S16;
int audio_channels = 1;
int audio_buffers = 4096;
if ( SDL_InitSubSystem(SDL_INIT_AUDIO) < 0 ) {
fprintf(stderr, "Unable to initialize SDL_AUDIO: %s\n", SDL_GetError());
return;
}
if( Mix_OpenAudio( audio_rate, audio_format, audio_channels, audio_buffers ) == -1 ){
printf("Oh My Goodness, an error : %s", Mix_GetError());
iMusic = false; iSound = false;
return;
} else {
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
}
Mix_AllocateChannels(9);
iMusic = true; iSound = true;
lastRew = SDL_GetTicks();
}
SoundService::~SoundService(){ }
Mix_Chunk* SoundService::loadSound( std::string filename ){
if( !iSound ) return NULL;
Mix_Chunk* loadedSound = NULL;
loadedSound = Mix_LoadWAV( filename.c_str() );
if( loadedSound == NULL ){
printf("Oh My Goodness, an error : %s", Mix_GetError());
}
return loadedSound;
}
void SoundService::playSound( Mix_Chunk* sound ){
if( !iSound ) return;
int i;
for( i = 0; i < 9; i++){
if( Mix_Playing(i) == 0 ){
if( Mix_PlayChannel( i, sound, 0 ) == -1){
printf("Oh My Goodness, an error : %s", Mix_GetError());
}
break;
}
}
}
void SoundService::loadMusic( std::string filename ){
if( !iMusic ) return;
if( Mix_PlayingMusic() == 1 ){
Mix_HaltMusic();
Mix_FreeMusic( music );
} else if( music ){
Mix_FreeMusic( music );
}
music = NULL;
music = Mix_LoadMUS( filename.c_str() );
if( music == NULL ){
printf("Oh My Goodness, an error : %s", Mix_GetError());
}
}
void SoundService::playMusic(){
if( !iMusic ) return;
if( Mix_PlayingMusic() == 1 ){
Mix_HaltMusic();
}
if( Mix_PlayMusic( music, -1 ) == -1 ){
printf("Oh My Goodness, an error : %s", Mix_GetError());
}
}
void SoundService::pauseMusic(){
if( !iMusic ) return;
if( Mix_PausedMusic() == 0 ){
Mix_PauseMusic();
} else {
Mix_ResumeMusic();
}
}
void SoundService::soundKill(){
Mix_CloseAudio();
}
after the while cycle:
soundServ.loadMusic( "music.ogg" );
and
soundServ.playMusic();
and after the while cycle:
soundKill();
i also tryed MP3, but no success. this is realy giving me a hard time, all the sound functions are working (dont mind about my strange playSound()... it has a purpose).
any kind of advices would be great.
stay cool and cya arround.