Problems with SDL_mixer

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Decinoge
Posts: 4
Joined: Mon Nov 12, 2007 11:28 am

Problems with SDL_mixer

Post by Decinoge »

Hi everyone. I've just finished making the services (display, input and sound) for my application. However, my sound service is giving me no music.
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 &#40; SDL_InitSubSystem&#40;SDL_INIT_AUDIO&#41; < 0 &#41; &#123;
          fprintf&#40;stderr, "Unable to initialize SDL_AUDIO&#58; %s\n", SDL_GetError&#40;&#41;&#41;;
          return;
     &#125;
     
     if&#40; Mix_OpenAudio&#40; audio_rate, audio_format, audio_channels, audio_buffers &#41; == -1 &#41;&#123;
         printf&#40;"Oh My Goodness, an error &#58; %s", Mix_GetError&#40;&#41;&#41;;
         iMusic = false; iSound = false;
         return;
     &#125; else &#123;
         Mix_QuerySpec&#40;&audio_rate, &audio_format, &audio_channels&#41;;
     &#125;
     
     Mix_AllocateChannels&#40;9&#41;;
     iMusic = true; iSound = true;
     lastRew = SDL_GetTicks&#40;&#41;;
&#125;

SoundService&#58;&#58;~SoundService&#40;&#41;&#123; &#125;

Mix_Chunk* SoundService&#58;&#58;loadSound&#40; std&#58;&#58;string filename &#41;&#123;
     if&#40; !iSound &#41; return NULL;
     Mix_Chunk* loadedSound = NULL;
     
     loadedSound = Mix_LoadWAV&#40; filename.c_str&#40;&#41; &#41;;
     
     if&#40; loadedSound == NULL &#41;&#123;
         printf&#40;"Oh My Goodness, an error &#58; %s", Mix_GetError&#40;&#41;&#41;;
     &#125;
     
     return loadedSound;
&#125;

void SoundService&#58;&#58;playSound&#40; Mix_Chunk* sound &#41;&#123;
     if&#40; !iSound &#41; return;
     int i;
     for&#40; i = 0; i < 9; i++&#41;&#123;
          if&#40; Mix_Playing&#40;i&#41; == 0 &#41;&#123;
              if&#40; Mix_PlayChannel&#40; i, sound, 0 &#41; == -1&#41;&#123;
                  printf&#40;"Oh My Goodness, an error &#58; %s", Mix_GetError&#40;&#41;&#41;;
              &#125;
              break;
          &#125;
     &#125;
     
&#125;

void SoundService&#58;&#58;loadMusic&#40; std&#58;&#58;string filename &#41;&#123;
     if&#40; !iMusic &#41; return;
     if&#40; Mix_PlayingMusic&#40;&#41; == 1 &#41;&#123;
         Mix_HaltMusic&#40;&#41;;
         Mix_FreeMusic&#40; music &#41;;
     &#125; else if&#40; music &#41;&#123;
         Mix_FreeMusic&#40; music &#41;;
     &#125;
     
     music = NULL;
     music = Mix_LoadMUS&#40; filename.c_str&#40;&#41; &#41;;
     
     if&#40; music == NULL &#41;&#123;
         printf&#40;"Oh My Goodness, an error &#58; %s", Mix_GetError&#40;&#41;&#41;;
     &#125;
&#125;

void SoundService&#58;&#58;playMusic&#40;&#41;&#123;
     if&#40; !iMusic &#41; return;
     if&#40; Mix_PlayingMusic&#40;&#41; == 1 &#41;&#123;
         Mix_HaltMusic&#40;&#41;;
     &#125;
     
     if&#40; Mix_PlayMusic&#40; music, -1 &#41; == -1 &#41;&#123;
         printf&#40;"Oh My Goodness, an error &#58; %s", Mix_GetError&#40;&#41;&#41;;
     &#125;
&#125;

void SoundService&#58;&#58;pauseMusic&#40;&#41;&#123;
     if&#40; !iMusic &#41; return;
     if&#40; Mix_PausedMusic&#40;&#41; == 0 &#41;&#123;
         Mix_PauseMusic&#40;&#41;;
     &#125; else &#123;
         Mix_ResumeMusic&#40;&#41;;
     &#125;
&#125;

void SoundService&#58;&#58;soundKill&#40;&#41;&#123;
     Mix_CloseAudio&#40;&#41;;
&#125;
the only thing i do on the main is:

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.
none
Post Reply