1
Audio / [SOLVED] No sound is played but in debug mode is played
« on: April 20, 2021, 07:28:11 pm »
Hello!
I'm developing a small game and get to the point audio to be introduced to the game. But when I run the game and when sound should be played the game play no sound. The strange thing is when I'm in debug a sound is played when I go stepping through the code. Nothing as error is shown to the console.
Here is a simplified scheme how I organized the audio system so far:
Some explanations on the code above:
What am I missing?
I'm developing a small game and get to the point audio to be introduced to the game. But when I run the game and when sound should be played the game play no sound. The strange thing is when I'm in debug a sound is played when I go stepping through the code. Nothing as error is shown to the console.
Here is a simplified scheme how I organized the audio system so far:
// singelton class for holding of all of the resources
class ResourcesManager
{
public:
static ResourcesManager* getInstance(); // return single instance
std::vector<sf::SoundBuffer> getSoundBuffers();
...
private:
ResourcesManager() {}
...
std::vector<sf::SoundBuffer> soundBuffers;
}
// another singelton class for playing the sounds
class SoundsPlayer
{
public:
static std::shared_ptr<SoundsPlayer> getInstance();
void play(const sf::SoundBuffer& buffer)
{
bool soundPlayed = false;
// get next available sound
for (auto sound : m_sounds)
{
if (sound.getStatus() != sf::SoundSource::Status::Playing)
{
sound.setBuffer(buffer);
sound.play();
soundPlayed = true;
break;
}
}
if (!soundPlayed) // no sounds available
{
sf::Sound sound;
m_sounds.push_back(sound);
size_t last = m_sounds.size() - 1;
m_sounds[last].setBuffer(buffer);
m_sounds[last].play();
}
}
private:
SoundsPlayer() = default;
std::vector<sf::Sound> m_sounds;
};
class PlayingCharacter
{
...
void playAttackingSound()
{
extern ResourcesManager *resMan;
SoundBuffersHolder soundBuffersHolder;
std::shared_ptr<SoundsPlayer> soundsPly = SoundsPlayer::getInstance();
bool isErr;
soundsPly->play(resMan->getSoundBuffers().soundBuffers[0]);
}
}
class ResourcesManager
{
public:
static ResourcesManager* getInstance(); // return single instance
std::vector<sf::SoundBuffer> getSoundBuffers();
...
private:
ResourcesManager() {}
...
std::vector<sf::SoundBuffer> soundBuffers;
}
// another singelton class for playing the sounds
class SoundsPlayer
{
public:
static std::shared_ptr<SoundsPlayer> getInstance();
void play(const sf::SoundBuffer& buffer)
{
bool soundPlayed = false;
// get next available sound
for (auto sound : m_sounds)
{
if (sound.getStatus() != sf::SoundSource::Status::Playing)
{
sound.setBuffer(buffer);
sound.play();
soundPlayed = true;
break;
}
}
if (!soundPlayed) // no sounds available
{
sf::Sound sound;
m_sounds.push_back(sound);
size_t last = m_sounds.size() - 1;
m_sounds[last].setBuffer(buffer);
m_sounds[last].play();
}
}
private:
SoundsPlayer() = default;
std::vector<sf::Sound> m_sounds;
};
class PlayingCharacter
{
...
void playAttackingSound()
{
extern ResourcesManager *resMan;
SoundBuffersHolder soundBuffersHolder;
std::shared_ptr<SoundsPlayer> soundsPly = SoundsPlayer::getInstance();
bool isErr;
soundsPly->play(resMan->getSoundBuffers().soundBuffers[0]);
}
}
Some explanations on the code above:
- ResourcesManager is taking care of holding all of the resources and it is a singelton class. So far it works the way I organized it for the textures and other resources - I invoke
extern ResourcesManager *resMan;
and then I get what I need from it, i.e.
sf::texture tex = resMan->getTexture() ) - First I simply tried to play a sound directly in the PlayingCharacter::playAttackingSound() method but there was no sound produced and I decided that probably the sf::Sound variable goes out of scope and that's why I made this SoundsPlayer singelton
- The PlayingCharacter::playAttackingSound() is invoked on more than a seconds (timer is taking care of it) and the sound is less than a second long so no overlapping happens
- Of course, these classes are in different files each
What am I missing?