SFML community forums

Help => Audio => Topic started by: Mind Calamity on November 20, 2011, 02:20:25 pm

Title: Playing a sound without a while loop ?
Post 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:
Code: [Select]

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]
Title: Playing a sound without a while loop ?
Post by: Laurent on November 20, 2011, 02:31:46 pm
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 ;)
Title: Playing a sound without a while loop ?
Post by: Mind Calamity on November 20, 2011, 03:21:12 pm
Well, that worked, I put it into a pointer, thanks for the tip, and the quick reply.

Here's the working code:

Code: [Select]
sf::Music* Music = new sf::Music();
Music->OpenFromFile(song);
Music->Play();
Title: Playing a sound without a while loop ?
Post by: Laurent on November 20, 2011, 04:55:13 pm
Don't forget to delete it when it's finished.