Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Best way to keep a music track running inside a thread?  (Read 2647 times)

0 Members and 2 Guests are viewing this topic.

the__cows

  • Newbie
  • *
  • Posts: 7
    • View Profile
Best way to keep a music track running inside a thread?
« 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Best way to keep a music track running inside a thread?
« Reply #1 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.
« Last Edit: July 08, 2015, 09:39:03 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

the__cows

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Best way to keep a music track running inside a thread?
« Reply #2 on: July 10, 2015, 07:54:27 pm »
Ahhhh thank you! :D

 

anything