Hey guys, what's up,
So I've been following the tutorials on playing sounds, and I've run into a kind of weird issue that doesn't seem to be mentioned anywhere: The sound doesn't play, and when the program ends I get this error: "AL lib: (EE) alc_cleanup: 1 device not closed". So after a bit of Googlefishing, I found a few sites where people have asked about it, and even one guy who filed a bug about it - but since I'm new to SFML I'm assuming it's something I've done wrong.
So here's my code:
#ifndef SOUND_PLAYER_H
#define SOUND_PLAYER_H
#include <map>
#include <SFML/Audio.hpp>
class SoundPlayer
{
public:
static SoundPlayer * Get();
void PlayerLaser();
private:
static SoundPlayer * Instance;
SoundPlayer();
sf::SoundBuffer player_laser;
};
SoundPlayer * SoundPlayer::Instance = NULL;
SoundPlayer * SoundPlayer::Get()
{
if (Instance == NULL)
Instance = new SoundPlayer();
return Instance;
}
SoundPlayer::SoundPlayer()
{
if (!player_laser.loadFromFile("bin\\debug\\sounds\\player_laser.wav"))
throw "Error loading the player laser sound";
}
void SoundPlayer::PlayerLaser()
{
sf::Sound sound;
sound.setBuffer(player_laser);
sound.play();
}
#endif
// And here is how it's used:
SoundPlayer * sp = SoundPlayer::Get();
sp->PlayerLaser();
The SoundPlayer class is mostly an attempt to keep the sound-related objects in scope (since stuff seems to go out of scope and get destroyed super-easy/almost instantly with SFML), but originally it was the constructor and PlayerLaser methods as one function. I figured that was not efficient (cuz it would have to re-load-from-file every time the sound is played), but I figured I'd share it anyway.
But this error seems to suggest that something needs closing? I didn't see anything in the tutorial about that (apart from if you have too many sounds, but I can't get it to play just one). I haven't managed to find any clear answer on Google or in the API docs, but then again I only spent like half an hour fighting with it. Has anyone here dealt with this cryptic error before? No big deal (I can always use IrrKlang for sounds if I can't get past it) but I'm guessing it's another newbie mistake on my part and a common pitfall everybody fights with at some point.
Also, if there is a better way to make references to SFML objects permanent, or at least exist a little longer, I'm all ears. I recently had a similar struggle with textures, and learned the hard way that storing them in a vector was a dumb idea. I know others have found or built workarounds, but if it's basic C++ stuff it really is something I should learn. I know what a pointer is, but I've never had as many issues with pointers being destroyed as I have with SFML; but I'm sure SFML isn't the only framework to auto-destroy pointers, so I "better figger it out fast" tyvm