SDL_Mixer: How to get music position?

Started by
2 comments, last by eruption_sword 16 years, 3 months ago
I'm writing a game in which I need to play one song, then play a second song, and afterwards play the first song again from the point at which it left off. The problem is, it doesn't seem SDL_Mixer has a way of returning the current music position. Is there any way around this? Thanks.
Advertisement
If SDL_Mixer doesn't have some function to return the position of your song, you can always use SDL_GetTicks and other functions in order to create your own timer to implement in your music class. I assume that if you can have a timer on each song, you can simply pause the timer, and using this return to the last point where your music left off. I'm not too sure though, I've never used SDL_Mixer, but this seems like a solution to me. Good luck!
[edit: the above idea is probably better. It will probably get decent results, besides being easier to implement]


The SDL_Mixer documentation seems to be lacking, at least on the doc wiki. Viewing the SDL_mixer include file I have found a way of doing this - perhaps.

SDL_mixer has a whole bunch of callbacks, but only one capable of doing what you want. You can use this function:
Mix_RegisterEffect(int chan, Mix_EffectFunc_t f,					Mix_EffectDone_t d, void *arg);


To register a function like so:
struct Music {   int currentPosition;   Mix_Music *music;};void musicLengthCallback(int chan, void *stream, int len, void *udata){    Music *music = static_cast<Music *>(udata);    music->currentPosition += len;}void pauseMusic(){   // whatever else needs doing      int channel = /* whatever channel music plays in */;   Mix_UnregisterEffect(channel, &musicLengthCallback);}void resumeMusic( Music * music ){    int channel = /* whatever channel music plays in */;        Mix_RegisterEffect(channel, &musicLengthCallback,0, music);    double position = /*    figure out the below based on music->currentPosition    from SDL_Mixer header:     This function is only implemented for MOD music formats (set pattern   order number) and for OGG music (set position in seconds), at the   moment.    */    Mix_SetMusicPosition(position);    // whatever else needs doing}


I can't test this, but it might work. No other function appears to have the functionality you need.
Well I finally got it to work after implmenting my own timer as FernandoLujan suggested. Thanks everyone.

This topic is closed to new replies.

Advertisement