Hi,
Having some problems with playing a sound. I have a small audio component that handles the playing of sounds:
AudioComponent.h#pragma once
#include "Component.h"
#include <SFML\Audio.hpp>
#include <memory>
class AudioComponent : public Component
{
public:
/**
* Default Constructor.
*/
AudioComponent();
/**
* Sets the sound buffer.
* @param buffer An existing buffer wrapped in a unique pointer.
* @return True if successfully.
*/
bool SetSoundBuffer(std::unique_ptr<sf::SoundBuffer> buffer);
/**
* Sets the sound buffer.
* @param filePath The location of the sound file to load.
* @return True if successfully.
*/
bool SetSoundBuffer(std::string filePath);
/**
* Plays the sound that's currently set.
*/
void Play();
private:
/**
* The components sound buffer.
*/
std::unique_ptr<sf::SoundBuffer> buffer;
/**
* The main sound object.
*/
sf::Sound sound;
};
AudioComponent.cpp#include "AudioComponent.h"
/** Default Constructor. */
AudioComponent::AudioComponent()
{
this->buffer = std::make_unique<sf::SoundBuffer>();
}
/** Sets the sound buffer. */
bool AudioComponent::SetSoundBuffer(std::unique_ptr<sf::SoundBuffer> buffer)
{
this->buffer = std::move(buffer);
this->sound.setBuffer(*this->buffer);
return true;
}
/** Sets the sound buffer. */
bool AudioComponent::SetSoundBuffer(std::string filePath)
{
if (this->buffer->loadFromFile(filePath))
{
this->sound.setBuffer(*this->buffer);
return true;
}
else
{
return false;
}
}
/** Plays the sound that's currently set. */
void AudioComponent::Play()
{
this->sound.play();
}
This code doesn't work, and no sound is played. After some playing I've found it's returning from the Play() function that is stopping the sounds. It took me a while to look there as I understand sf::Sound and sf::Music both create their own threads. If I hang around in the Play() function by appending a while statement at the end:
/** Plays the sound that's currently set. */
void AudioComponent::Play()
{
this->sound.play();
while(true){};
}
As I understand it, a common reason for this is that the sound buffer goes out of scope and gets destroyed, but it's a member variable wrapped in a std::unique_ptr in this case.
Can anybody shed any light here? There must be something I'm missing as to why it's stopping when the function returns.