Song switching

Add these member variables to your Player class:
song *next_song
bool swapping_songs

Recreate play_song like this:

Player::play_song(Song *s) {
next_song = s;

if (swapping_songs) return;
swapping_songs = true;

if (current_song) {
->call SDL_Music stop
->free current_song via SDL_Mixer
}

while (next_song)
{
s = next_song;
next_song = NULL;

->allocate SDL_Music Handle by s->Path()
->store the new handle in current_song
->play current_song via SDL_Mixer

}

swapping_songs = false;
}

//====================================================

Thanks !
That would make the method actually thread safe,
wouldnt it ?A nice feature to add, but for now my
program still does everything in one thread, except
for SDL_Mixer and its subthreads of course. (Next time
i’ll mention it earlier).So there will never be calls
to play_song at the same time. The error still occurs
when literally writing this in the source code:

play_song(a);
play_song(b);

Always when there is almost no time between those
calls it becomes more likely that I get a SEGFAULT.
But before play_song(a) isnt done, play_song(b) cannot
be executed. So the song_switching bool doesnt help.
That is why I thought that SDL_Mixer is not able (or
should not be able) to handle such fast changes of the
music source. (Its all Mixers’s fault !:))

The solution I’ve come to and which seems to work is
if i have a kind of deletion waiting queue. Its 8
songs long so if the 9th song is played the player
frees the first played song with Mix_FreeMusic. Memory
overhead is neglegtable. And when killing the player
instance the player kills every song in the waiting
queue.

Thank you,
Marc___________________________________________________________
Bestellen Sie Y! DSL und erhalten Sie die AVM “FritzBox SL” f?r 0?.
Sie sparen 119? und bekommen 2 Monate Grundgeb?hrbefreiung.
http://de.adsl.yahoo.com