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

Author Topic: Playing a sound without a while loop ?  (Read 3580 times)

0 Members and 1 Guest are viewing this topic.

Mind Calamity

  • Newbie
  • *
  • Posts: 10
    • View Profile
Playing a sound without a while loop ?
« 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]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Playing a sound without a while loop ?
« Reply #1 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 ;)
Laurent Gomila - SFML developer

Mind Calamity

  • Newbie
  • *
  • Posts: 10
    • View Profile
Playing a sound without a while loop ?
« Reply #2 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();

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Playing a sound without a while loop ?
« Reply #3 on: November 20, 2011, 04:55:13 pm »
Don't forget to delete it when it's finished.
Laurent Gomila - SFML developer