Commit f78c01fa authored by Fabio Bas's avatar Fabio Bas
Browse files

Avoid multiple sounds playing at the same time

parent 85aa866e
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
#define TEST_SOUND_FILENAME "player_join" #define TEST_SOUND_FILENAME "player_join"
SoundEngine::SoundEngine(QObject *parent) SoundEngine::SoundEngine(QObject *parent)
: QObject(parent), enabled(false) : QObject(parent), engine(0)
{ {
ensureThemeDirectoryExists(); ensureThemeDirectoryExists();
connect(settingsCache, SIGNAL(soundThemeChanged()), this, SLOT(themeChangedSlot())); connect(settingsCache, SIGNAL(soundThemeChanged()), this, SLOT(themeChangedSlot()));
...@@ -43,6 +43,15 @@ SoundEngine::SoundEngine(QObject *parent) ...@@ -43,6 +43,15 @@ SoundEngine::SoundEngine(QObject *parent)
themeChangedSlot(); themeChangedSlot();
} }
SoundEngine::~SoundEngine()
{
if(engine)
{
delete engine;
engine = 0;
}
}
void SoundEngine::soundEnabledChanged() void SoundEngine::soundEnabledChanged()
{ {
if (settingsCache->getSoundEnabled()) { if (settingsCache->getSoundEnabled()) {
...@@ -76,7 +85,19 @@ void SoundEngine::playSound(QString fileName) ...@@ -76,7 +85,19 @@ void SoundEngine::playSound(QString fileName)
if(!fi.exists()) if(!fi.exists())
return; return;
QSound::play(fi.absoluteFilePath()); if(engine)
{
if(engine->isFinished())
{
engine->stop();
delete engine;
} else {
return;
}
}
engine = new QSound(fi.absoluteFilePath());
engine->play();
} }
void SoundEngine::testSound() void SoundEngine::testSound()
......
...@@ -6,16 +6,20 @@ ...@@ -6,16 +6,20 @@
#include <QDir> #include <QDir>
#include <QString> #include <QString>
class QSound;
typedef QMap<QString, QString> QStringMap; typedef QMap<QString, QString> QStringMap;
class SoundEngine : public QObject { class SoundEngine : public QObject {
Q_OBJECT Q_OBJECT
public: public:
SoundEngine(QObject *parent = 0); SoundEngine(QObject *parent = 0);
~SoundEngine();
void playSound(QString fileName); void playSound(QString fileName);
QStringMap &getAvailableThemes(); QStringMap &getAvailableThemes();
private: private:
bool enabled; bool enabled;
QSound * engine;
QStringMap availableThemes; QStringMap availableThemes;
protected: protected:
void ensureThemeDirectoryExists(); void ensureThemeDirectoryExists();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment