SFML community forums

Help => Audio => Topic started by: zakkor on February 21, 2014, 02:08:21 pm

Title: Sound stops playing once sf::Sound object goes out of scope?
Post by: zakkor on February 21, 2014, 02:08:21 pm
#ifndef PLAY_HPP_INCLUDED
#define PLAY_HPP_INCLUDED

void Game::play(std::string soundName)
{
    std::cout << "Loading sounds...\n";
    if (!buffer.loadFromFile(soundName))
        std::cout << "Error: Could not find sound file named " << soundName << " !\n";

    sf::Sound sound;
    sound.setBuffer(buffer);
    sound.play();
    std::cout << "Playing \"" << soundName << "\"\n";
}

#endif // PLAY_HPP_INCLUDED
 

'buffer' is declared outside, and is still in scope when play() is called, but the sound doesn't play. any ideas?
Title: Re: Sound stops playing once sf::Sound object goes out of scope?
Post by: zakkor on February 21, 2014, 02:09:38 pm
Nevermind. It seems the sf::Sound object also needs to be alive for the sound to play.
Title: Re: Sound stops playing once sf::Sound object goes out of scope?
Post by: Nexus on February 21, 2014, 02:22:58 pm
Yes, and this is stated in the documenation/tutorial :)

I recommend using std::queue<sf::Sound> to store the playing sounds.