Original Post
I recently added SDL_mixer to our game to play sound effects. Unfortunately for some reason if I call Mix_PlayerChannel() to play a Mix_Chunk that chunk will loop forever until the process is closed, even if I Mix_FreeChunk() the sound. At first I thought it might be something in the game, so I created the test case below and still have the issue. I also thought it may be my sound drivers but our designer gets the exact same issue. I noticed that if I change the audioBuffers size it changes how often it repeats. For example if I set audioBuffers to 1024 instead of 4096 it repeats a lot quicker. This makes me think that SDL_mixer is just looping the buffer it creates. This is my first time using SDL_mixer so I wondering if I've missed something? Some update call to SDL_mixer for instance? Although I didn't notice anything else in any of the tutorials/docs. I've also tried adding a while(Mix_Playing(channel) != 0); instead of while(1) in the code below and it quits after the sound is finished, so it detects the end of the sound correctly. The version of SDL/SDL_mixer we're using is the latest 1.3 trunk from their subversion. Any help appreciated [smile].
#include <SDL.h>
#include <SDL_mixer.h>
#include <iostream>
int main(int argc, char* argv[])
{
// Initialise SDL
SDL_Init(SDL_INIT_EVERYTHING);
int audioRate = 22050;
Uint16 audioFormat = AUDIO_S16SYS;
int audioChannels = 2;
int audioBuffers = 4096;
// Open audio device.
int res = Mix_OpenAudio( audioRate, audioFormat, audioChannels, 4096 );
if( res == -1 )
{
char* errorMsg;
errorMsg = Mix_GetError();
std::cout << "Mix_OpenAudio(): failed - " << errorMsg << std::endl;
return 0;
}
std::cout << "Mix_OpenAudio(): success." << std::endl;
// Load a sound.
Mix_Chunk *sound = NULL;
sound = Mix_LoadWAV( "c:\\users\\public\\sound.wav" );
if(sound == NULL)
{
std::cout << "Mix_LoadWAV(): failed" << std::endl;
return 0;
}
std::cout << "Mix_LoadWAV(): success" << std::endl;
// Attempt to play the sound.
int channel;
channel = Mix_PlayChannel( -1, sound, 0 );
if(channel == -1)
std::cout << "Mix_PlayChannel(): failed" << std::endl;
else
std::cout << "Mix_PlayChannel(): success. channel: " << channel << std::endl;
while(Mix_Playing(channel) != 0)
{
std::cout << "Mix_PlayChannel(): Playing" << std::endl;
}
Mix_FreeChunk(sound);
std::cout << "Mix_FreeSound(): Done" << std::endl;
while( 1);
Mix_CloseAudio();
return 0;
}