Sign in to follow this  

SDL_Mixer: How to get music position?

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this