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

Author Topic: Sound in Class function vs Sound in Main function  (Read 2442 times)

0 Members and 1 Guest are viewing this topic.

Lurnon

  • Newbie
  • *
  • Posts: 1
    • View Profile
Sound in Class function vs Sound in Main function
« on: May 25, 2013, 01:46:33 am »
Hello,
everybody.
I use the following code
sf::SoundBuffer SB;
SB.loadFromFile("Sounds\\Richtig.ogg");
sf::Sound Sound(SB);
Sound.play();

One time I use it in the main loop of my application the other time in a function of one of my classes. Both use the exact same for lines of code. The problem is the sound in the main loop is played correctly. In my class function I don't hear anything. I checked the return value of
SB.loadFromFile("Sounds\\Richtig.ogg");
and shows me no problem. But still I can't hear any sound. Does somebody know the
reason for that?

Best regards
Lurnon

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sound in Class function vs Sound in Main function
« Reply #1 on: May 25, 2013, 07:39:21 am »
If the sf::Sound and sf::SoundBuffer instances are local to the function, they are destroyed at the end of it. You must keep them alive (for example, as class members) as long as they are being played.
Laurent Gomila - SFML developer

 

anything