SFML community forums

Help => Audio => Topic started by: DZeeSea on March 03, 2013, 06:16:32 pm

Title: [solved]sf::SoundBuffer/ Sound not hearing sound
Post by: DZeeSea on March 03, 2013, 06:16:32 pm
I'm already using sf::Music to play a streamed background song in my game and it works well. For some reason I can't play any sound effects trough sf::Sound.

Here's a sample of different classes :

Resource Manager(RM) is a singleton holding a SoundBuffer member :


sf::SoundBuffer& RM::hitSoundBuffer()
{
        return _hitSound;
}

RM::RM(void) :
                        SPLASH_SCREEN_PATH("res/texture.png"),
                        START_BUTTON_PATH("res/play.png"),
                        BOARD_PATH("res/board.png"),
                        BLOCKS_PATH("res/blockColors.png"),
                        PLAYSTATE_BG("res/playstate_wallpaper.png"),
                        MUSIC_BG("res/bgMusic.ogg"),
                        SOUND_HIT("res/hit.ogg")
{      
        _splashScreenTexture.loadFromFile(SPLASH_SCREEN_PATH);
        _startButtonTexture.loadFromFile(START_BUTTON_PATH);
        _boardTexture.loadFromFile(BOARD_PATH);
        _blocksTexture.loadFromFile(BLOCKS_PATH);
        _playStateBG.loadFromFile(PLAYSTATE_BG);
        _bgMusic.setLoop(true);
        _bgMusic.setVolume(50.0f);
        _bgMusic.openFromFile(MUSIC_BG);
        _hitSound.loadFromFile(SOUND_HIT);
}
 


Here's where I call it :

                        bool canMoveDown = _gameGrid.attemptPeriodicShapeMoveDown(_droppingShape);
                       
                        if(!canMoveDown)
                        {
                                //Once we put the shape down we,
                                //-Delete the falling shape since it's imprinted in the grid.
                                //-We possibly clear filled rows by the player.
                                //-We check for a loss.
                                //-Sound on hit.
                                delete _droppingShape;
                                _droppingShape = NULL;

                                sf::Sound hitSound(RM::getInstance()->hitSoundBuffer());
                                hitSound.play();

                                _gameGrid.possiblyClearRows();
                                _gameOver = _gameGrid.checkForLoss();
                        }

No idea why I'm not hearing a sound. I tried swapping audio files to no avail. This is 2.0 in VS.
Title: Re: sf::SoundBuffer/ Sound not hearing sound
Post by: G. on March 03, 2013, 06:37:20 pm
Your hitSound variable is deleted at the end of its scope. Even if it's playing.
Title: AW: Re: sf::SoundBuffer/ Sound not hearing sound
Post by: eXpl0it3r on March 03, 2013, 10:25:10 pm
I'm going to create a global sound object and reset it's buffer for every sound in my game then.
Don't use globals, you'll only make your life harder.
Just think of a better resource management design and keep things at least local to a class.