SFML community forums

Help => Audio => Topic started by: the__cows on July 08, 2015, 09:33:07 pm

Title: Best way to keep a music track running inside a thread?
Post by: the__cows on July 08, 2015, 09:33:07 pm
Hi,

So I'm basically creating a thread for music with the code;

void startMusic() {
        sf::Music music;
        music.openFromFile("upandaway.ogg");
        music.setLoop(true);
        music.play();

        while (music.getStatus() == sf::Music::Status::Playing) {}
}

sf::Thread thread(&startMusic)

Obviously a while loop is not a viable way due to very high memory usage (tried it, and it was using 50% of my CPU. 50% for music!)

What would be an alternative to this, to keep the music running while keeping CPU Usage relatively low?
Title: Re: Best way to keep a music track running inside a thread?
Post by: eXpl0it3r on July 08, 2015, 09:35:24 pm
sf::Music as well as sf::Sound create their own thread, so you can just call play() and they will play as long as there's sound to play. No need for your own threads.

If you ever have a loop where you're just basically waiting, then you could use sf::sleep() to put the thread to sleep for a certain time duration.
Title: Re: Best way to keep a music track running inside a thread?
Post by: the__cows on July 10, 2015, 07:54:27 pm
Ahhhh thank you! :D