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

Author Topic: Sound only plays in event loop  (Read 3000 times)

0 Members and 1 Guest are viewing this topic.

atac57

  • Newbie
  • *
  • Posts: 9
    • View Profile
Sound only plays in event loop
« on: September 20, 2013, 06:30:00 am »
This is some test code I wrote for an alarm clock program. It seems that the sound object only plays if its inside the inner while loop. If I move the mouse cursor in and out of the window, the sound restarts. I only want the sound to play if the time is equal to a time that I want the sound to go off. I wrote a function for that (not shown here) and the function works, so thats not the problem. Why can't sound play outside of an event loop?
int main(){
        sf::RenderWindow window(sf::VideoMode(400,400),"");
        sf::SoundBuffer buffer;
        sf::Sound sound;
        sf::Event event;
       
        if(!buffer.loadFromFile("alarmClock.ogg"))
                return EXIT_FAILURE;
               
        sound.setBuffer(buffer);
       
        while(window.isOpen()){
                while(window.pollEvent(event)){
                        if(event.type == sf::Event::Closed){
                                window.close();
                        }
                        sound.play();
                }
                //sound.play() <--- doesn't play here
                window.display();
        }
}

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Sound only plays in event loop
« Reply #1 on: September 20, 2013, 07:13:26 am »
Quoth the documentation:
Quote
void sf::Sound::play   (      )   
Start or resume playing the sound.

This function starts the stream if it was stopped, resumes it if it was paused, and restarts it from beginning if it was it already playing.

So odds are it is playing the sound, it just restarts so many times per second that you only ever "hear" the first fraction of a second of it.  Try testing with something that won't restart it constantly, like if(sound.getStatus() != sf::Sound::Playing) sound.play().

atac57

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Sound only plays in event loop
« Reply #2 on: September 20, 2013, 07:24:19 am »
That was it! Thank you so much!

 

anything