Sign in to follow this  
Storyyeller

SDL_Mixer GetMusicPosition

Recommended Posts

Storyyeller    215
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.

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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