SFML community forums

Help => Audio => Topic started by: atac57 on September 20, 2013, 06:30:00 am

Title: Sound only plays in event loop
Post by: atac57 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();
        }
}
Title: Re: Sound only plays in event loop
Post by: Ixrec 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().
Title: Re: Sound only plays in event loop
Post by: atac57 on September 20, 2013, 07:24:19 am
That was it! Thank you so much!