SFML community forums
Help => Audio => Topic started by: Mind Calamity on November 20, 2011, 02:20:25 pm
-
Um.. This is probably a stupid question, but the tutorials use a while loop which seems to play the sound (by checking it's status ??), and removing that stops the audio from being played...
So, how exactly should I go about playing sounds ?
In other words... This works:
sf::Music Music;
// char* song = "D:\\song.ogg";
Music.OpenFromFile(song);
Music.Play();
// Without the line below it doesn't work...
while (Music.GetStatus() == sf::Music::Playing) {}
And it also prevents anything else to be executed before the sound/music ends playing.
[/code]
-
No you don't need this loop to play the sound, it is played in a separate thread so the main one can do whatever it wants.
You just need to make sure that the sf::Music instance lives as long as you're playing it. If it is destroyed before, obviously the music will stop ;)
-
Well, that worked, I put it into a pointer, thanks for the tip, and the quick reply.
Here's the working code:
sf::Music* Music = new sf::Music();
Music->OpenFromFile(song);
Music->Play();
-
Don't forget to delete it when it's finished.