Wahoozel
Wahoozel

Reputation: 297

SFML Sound not playing

I'm trying to play a sound file in SFML but it's not playing. This is the sound (warning: loud volume): http://freesound.org/people/thanvannispen/sounds/9550/. I've used an online converter to convert it to wav (http://audio.online-convert.com/convert-to-wav) but the sound plays fine in windows so that shouldn't be the problem.

SoundManager.h

#pragma once

#include <SFML\Audio.hpp>
#include <map>

class SoundManager
{
public:
    SoundManager();
    ~SoundManager();

    static bool load();

    static std::map<std::string, sf::Sound> sound;
};

SoundManager.cpp

#include "SoundManager.h"
#include <SFML\Audio.hpp>
#include <map>

std::map<std::string, sf::Sound> SoundManager::sound = std::map<std::string, sf::Sound>();

SoundManager::SoundManager()
{
}

SoundManager::~SoundManager()
{
}

bool SoundManager::load() {
    sf::SoundBuffer laughterBuffer;
    if (!laughterBuffer.loadFromFile("data/sound/laughter.wav")) {
        return false;
    }
    sf::Sound laughter;
    laughter.setBuffer(laughterBuffer);
    SoundManager::sound["laughter"] = laughter;
}

Here I load the sound:

Logger::print("Loading sound...");
if (!SoundManager::load()) {
    Logger::error("Failed to load sound! Exiting...");
    exit();
}
Logger::print("Sound loaded.");

And I play it like this just before entering the game loop (I've tried calling it from multiple places; none work)

SoundManager::sound["laughter"].play();

When I first tried to import SFML\Audio.hpp it gave me an error about not finding libsndfile-1.dll, so I copy/pasted it into the folder and now it works, but I'm not sure why that happened since I'm linking statically. Could it have something to do with how I am linking to SFML?

sfml-main.lib
sfml-network-s.lib
ws2_32.lib
sfml-audio-s.lib
sndfile.lib
openal32.lib
sfml-window-s.lib
sfml-system-s.lib
opengl32.lib
gdi32.lib
winmm.lib
sfml-graphics-s.lib
glew.lib
freetype.lib
jpeg.lib

Upvotes: 2

Views: 6740

Answers (2)

Haha TTpro
Haha TTpro

Reputation: 5546

Not only sf::SoundBuffer, but sf::Sound also need to alive too.

You could set both of them as class variable or global variable (but not variable in a function because it will be killed after execution).

For example:

.h file

#pragma once
#include <SFML/Audio.hpp>
class SoundManager
{
private:
    sf::SoundBuffer missile_launch_buff;
    sf::Sound fire_sound;
public:
    SoundManager();
    void playMissileLaunch();

};

.cpp file

#include "SoundManager.h"
#include <iostream>
using namespace std;


SoundManager::SoundManager()
{

    missile_launch_buff.loadFromFile("sound/missile_launch.wav");

}

void SoundManager::playMissileLaunch()
{

    fire_sound.setBuffer(missile_launch_buff);
    fire_sound.play();
    cout << "Missile launch sound \n";
}

Upvotes: 0

Hiura
Hiura

Reputation: 3530

From SFML documentation about sf::Sound::setBuffer():

It is important to note that the sound buffer is not copied, thus the sf::SoundBuffer instance must remain alive as long as it is attached to the sound.

which is not the case with laughterBuffer.

I suggest having a look at Thor resources or sftools resources.

Upvotes: 2

Related Questions