SFML community forums

Help => Audio => Topic started by: DrenikowB on October 12, 2012, 02:10:43 am

Title: Event lag when music is playing
Post by: DrenikowB on October 12, 2012, 02:10:43 am
Hello,

I am having an issue with my SFML. I am new to programming with SFML and the issue I have is that when I run my loop for playing music I want to be able to pause the music. It works but it takes multiple attempts of pressing the key before it takes in the event and actually pauses the music. Does anyone know how to fix it? Thanks :D

Here's some code for what I'm trying to do.

        sf::Sound Sound(Buffer);
        while (App.IsOpened()){

                if(first ==true){
                        Sound.Play();
                        first =false;
                }
               
                // Loop while the sound is playing
                if (Sound.GetStatus() == sf::Sound::Playing)
                {
                        // Display the playing position
                        std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << Sound.GetPlayingOffset() << " sec";

                        // Leave some CPU time for other threads
                        sf::Sleep(0.1f);

                        while (App.GetEvent(Event))
               {
                                // Close window : exit
                                App.GetEvent(Event);
                                if (Event.Type == sf::Event::Closed){
                                        App.Close();
                                        Sound.Stop();
                                }

                                // Escape key : exit
                                if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)){
                                        App.Close();
                                        Sound.Stop();
                                }
                                if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key:: P)){
                                        Sound.Pause();
                                }

                                const sf::Input &input = App.GetInput();

                        }

                        App.Display();
                }
Title: Re: Event lag when music is playing
Post by: eXpl0it3r on October 12, 2012, 07:18:38 am
Please make use of the code=cpp tag in this forum.

I strongly advise you to use SFML 2. SFML 1.6 hasn't changed in over 2 years and contains many bugs and lacks quite a few features.

The problem with your code might be that you call GetEvent twice in a row, thus when only one event is triggered when the key is pressed it won't get proccessed. Only call GetEvent with the while statement.

Btw if(x==true) is useless, just write if(x) for the same effect. ;)