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

Author Topic: Sound stops playing once sf::Sound object goes out of scope?  (Read 2648 times)

0 Members and 1 Guest are viewing this topic.

zakkor

  • Newbie
  • *
  • Posts: 27
    • View Profile
Sound stops playing once sf::Sound object goes out of scope?
« 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?

zakkor

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Sound stops playing once sf::Sound object goes out of scope?
« Reply #1 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sound stops playing once sf::Sound object goes out of scope?
« Reply #2 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything