SDL_Mixer GetMusicPosition

Started by
3 comments, last by Shonumi 13 years, 4 months ago
I am using SDL_Mixer to play music in my game. I want to be able to play one track, then switch to a second piece of music, and then switch back to the original, and resume playing at the point it left off.

However, SDL_Mixer strangely seems to have no functions for getting the current position of the music.
I trust exceptions about as far as I can throw them.
Advertisement
As far as I can tell, yes, SDL_Mixer has no functions nor anything else within the API that let's you get the current track time. However, the solution should be pretty simple if you're willing to do it yourself. All you would need to do is keep track of time when the music starts until you need to change it. You would need to account for anything like pausing the music, of course. Timers, especially in SDL, are trivial enough to make.

You might want to encapsulate all of the functionality of playing, loading, pausing, and tracking songs into a dedicated class, assuming you are using an object oriented language. Managing music in SDL is a lot like managing sprites. Plenty of people write their own classes to handle the API to better suit the needs of gaming.
The timers don't seem to be very accurate though.
I trust exceptions about as far as I can throw them.
If you need sample-accurate timing, you probably won't be able to achieve that directly using SDL_mixer. However, SDL_mixer does provide a callback where you can mix the music yourself if you prefer. This would mean decompressing/streaming the audio yourself, but it would allow for sample-accurate timing and sequencing of tracks. (I used this approach to combine multiple sections into longer composite music tracks, and it seemed to work pretty well.)
Quote:Original post by Storyyeller
The timers don't seem to be very accurate though.


Really? Interesting. I haven't used SDL_Mixer practically yet (I'm not too good yet at making my own music yet...) but I figured the method I mentioned would at least be decent. I figured I'd do something like that whenever I got around to incorporating music for my game.

Though, what do you mean by the timers not being "very accurate" in terms of performance? Is not accurate in the sense that it can't pinpoint things like 6.25 seconds into a song, or is it drastically off by entire seconds? I'd just like to know what you're experiencing for future reference.

This topic is closed to new replies.

Advertisement