#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;
}
SDL_mixer: Mix_PlayChannel() loops sounds forever?
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].
That's pretty weird. What compiler are you using and on what system are you running?
Some things to try in no particular order: use a stable release as a sanity check instead of the 'latest 1.3 trunk from their subversion'. Try just SDL_INIT_AUDIO instead of SDL_INIT_EVERYTHING to rule out other SDL subsystems somehow conflicting. The documentation says about Mix_PlayChannel 'Note: this just calls Mix_PlayChannelTimed() with ticks set to -1.' Try using Mix_PlayChannelTimed instead and see if that works better. Try other sound resources with different formats if you haven't already.
Some things to try in no particular order: use a stable release as a sanity check instead of the 'latest 1.3 trunk from their subversion'. Try just SDL_INIT_AUDIO instead of SDL_INIT_EVERYTHING to rule out other SDL subsystems somehow conflicting. The documentation says about Mix_PlayChannel 'Note: this just calls Mix_PlayChannelTimed() with ticks set to -1.' Try using Mix_PlayChannelTimed instead and see if that works better. Try other sound resources with different formats if you haven't already.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!
I am not used to SDL but a quick guess is that when the audio is initialised it creates some buffers for the output audio. It will also create a timer and every so ofter it will fill up this buffer( the bigger the buffer, the less frequently it refill it).
The final while(1) in the example code is so that once the buffer has been mixed it does not quit out straight away.(Mix_CloseAudio - I guess will kill off the sound system) ( you have just copied the data into the out buffer and it has not played that yet).
Andrew.
The final while(1) in the example code is so that once the buffer has been mixed it does not quit out straight away.(Mix_CloseAudio - I guess will kill off the sound system) ( you have just copied the data into the out buffer and it has not played that yet).
Andrew.
Quote: Original post by seedy
I am not used to SDL but a quick guess is that when the audio is initialised it creates some buffers for the output audio. It will also create a timer and every so ofter it will fill up this buffer( the bigger the buffer, the less frequently it refill it).
The final while(1) in the example code is so that once the buffer has been mixed it does not quit out straight away.(Mix_CloseAudio - I guess will kill off the sound system) ( you have just copied the data into the out buffer and it has not played that yet).
Andrew.
The while(1) was just to keep the program running to hear the repeats. As I said I've added SDL_mixer to our games code and the problem occurs there too where there is a proper game loop etc.
Quote: Original post by nobodynews
That's pretty weird. What compiler are you using and on what system are you running?
Some things to try in no particular order: use a stable release as a sanity check instead of the 'latest 1.3 trunk from their subversion'. Try just SDL_INIT_AUDIO instead of SDL_INIT_EVERYTHING to rule out other SDL subsystems somehow conflicting. The documentation says about Mix_PlayChannel 'Note: this just calls Mix_PlayChannelTimed() with ticks set to -1.' Try using Mix_PlayChannelTimed instead and see if that works better. Try other sound resources with different formats if you haven't already.
Aha! I just grabbed the 1.2.14 SDL.dll from the website and the repeat is gone. I'm still using the SDL_mixer.dll I compiled myself. Unfortunately we need 1.3 for the iphone support.
Now I'm wondering if this is an SDL bug or if I'm compiling it incorrectly (very possible). I'm compiling from the 1.3 trunk with VC Express SP1 and the issue occurs on both XP and Vista. I noticed that my compiled release SDL.dll is ~850kb whereas the 1.2.14 SDL.dll from the website is only ~350kb. Could that mean I'm messing up somewhere?
Thanks for the replies.
Quote:Original post by seedy
I am not used to SDL but a quick guess is that when the audio is initialised it creates some buffers for the output audio. It will also create a timer and every so ofter it will fill up this buffer( the bigger the buffer, the less frequently it refill it).
The final while(1) in the example code is so that once the buffer has been mixed it does not quit out straight away.(Mix_CloseAudio - I guess will kill off the sound system) ( you have just copied the data into the out buffer and it has not played that yet).
Andrew.
The while(1) was just to keep the program running to hear the repeats. As I said I've added SDL_mixer to our games code and the problem occurs there too where there is a proper game loop etc.
Quote:Original post by nobodynews
That's pretty weird. What compiler are you using and on what system are you running?
Some things to try in no particular order: use a stable release as a sanity check instead of the 'latest 1.3 trunk from their subversion'. Try just SDL_INIT_AUDIO instead of SDL_INIT_EVERYTHING to rule out other SDL subsystems somehow conflicting. The documentation says about Mix_PlayChannel 'Note: this just calls Mix_PlayChannelTimed() with ticks set to -1.' Try using Mix_PlayChannelTimed instead and see if that works better. Try other sound resources with different formats if you haven't already.
Aha! I just grabbed the 1.2.14 SDL.dll from the website and the repeat is gone. I'm still using the SDL_mixer.dll I compiled myself. Unfortunately we need 1.3 for the iphone support.
Now I'm wondering if this is an SDL bug or if I'm compiling it incorrectly (very possible). I'm compiling from the 1.3 trunk with VC Express SP1 and the issue occurs on both XP and Vista. I noticed that my compiled release SDL.dll is ~850kb whereas the 1.2.14 SDL.dll from the website is only ~350kb. Could that mean I'm messing up somewhere?
Thanks for the replies.
I have exactly the same bug.
I use Windows 7 64bit, Visual Studio 2010, C++.
I use the default binary libraries I got from SDL websites (both SDL and SDL_mixer)
It keeps looping the sound effect over and over instead of playing it once (since 0 parameter).
However I change this parameter it keeps looping it forever.
Is it a SDL 1.3 bug?
Anyone?
Thanks,
I found out more strange behavior:
When I load a music (eg: an ogg vorbis) and play it with Mix_PlayMusic, it plays ok and also the sound effects play only one time.
But if I use Mix_FadeInMusic or even Mix_VolumeMusic then both sound effects are looping and music is playing wrongly (looping the first second forever).
When I load a music (eg: an ogg vorbis) and play it with Mix_PlayMusic, it plays ok and also the sound effects play only one time.
But if I use Mix_FadeInMusic or even Mix_VolumeMusic then both sound effects are looping and music is playing wrongly (looping the first second forever).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement