我'm trying to play a sound file in SFML but it'不玩了 . 这是声音(警告:响亮音量):http://freesound.org/people/thanvannispen/sounds/9550/ . 我已经使用在线转换器将其转换为wav(http://audio.online-convert.com/convert-to-wav),但声音在Windows中播放正常,这应该不是问题 .
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;
}
在这里我加载声音:
Logger::print("Loading sound...");
if (!SoundManager::load()) {
Logger::error("Failed to load sound! Exiting...");
exit();
}
Logger::print("Sound loaded.");
我在进入游戏循环之前就像这样玩它(我试过从多个地方调用它;没有工作)
SoundManager::sound["laughter"].play();
当我第一次尝试导入SFML \ Audio.hpp时,它给了我一个关于找不到libsndfile-1.dll的错误,所以我将它复制/粘贴到文件夹中,现在它可以工作了,但是我不知道为什么会发生这种情况,因为我静态链接 . 它可能与我如何链接到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
2 回答
从SFML文档有关
sf::Sound::setBuffer()
:laughterBuffer
不是这种情况 .我建议看看Thor resources或sftools resources .
不仅sf :: SoundBuffer,而且sf :: Sound也需要活着 .
您可以将它们都设置为类变量或全局变量(但在函数中不是变量,因为它将在执行后被终止) .
例如:
.h文件
.cpp文件