From f78c01fa88ae7c579366d831a49f3333a35834f8 Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Sun, 13 Sep 2015 17:22:58 +0200 Subject: [PATCH] Avoid multiple sounds playing at the same time --- cockatrice/src/soundengine.cpp | 27 ++++++++++++++++++++++++--- cockatrice/src/soundengine.h | 4 ++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/cockatrice/src/soundengine.cpp b/cockatrice/src/soundengine.cpp index 22d7578d..3865fd9b 100644 --- a/cockatrice/src/soundengine.cpp +++ b/cockatrice/src/soundengine.cpp @@ -33,7 +33,7 @@ #define TEST_SOUND_FILENAME "player_join" SoundEngine::SoundEngine(QObject *parent) -: QObject(parent), enabled(false) +: QObject(parent), engine(0) { ensureThemeDirectoryExists(); connect(settingsCache, SIGNAL(soundThemeChanged()), this, SLOT(themeChangedSlot())); @@ -43,6 +43,15 @@ SoundEngine::SoundEngine(QObject *parent) themeChangedSlot(); } +SoundEngine::~SoundEngine() +{ + if(engine) + { + delete engine; + engine = 0; + } +} + void SoundEngine::soundEnabledChanged() { if (settingsCache->getSoundEnabled()) { @@ -57,7 +66,7 @@ void SoundEngine::soundEnabledChanged() } #else qDebug("SoundEngine: enabling sound"); - enabled = true; + enabled = true; #endif } else { qDebug("SoundEngine: disabling sound"); @@ -76,7 +85,19 @@ void SoundEngine::playSound(QString fileName) if(!fi.exists()) 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() diff --git a/cockatrice/src/soundengine.h b/cockatrice/src/soundengine.h index ab9cc3d3..ba553c82 100644 --- a/cockatrice/src/soundengine.h +++ b/cockatrice/src/soundengine.h @@ -6,16 +6,20 @@ #include #include +class QSound; + typedef QMap QStringMap; class SoundEngine : public QObject { Q_OBJECT public: SoundEngine(QObject *parent = 0); + ~SoundEngine(); void playSound(QString fileName); QStringMap &getAvailableThemes(); private: bool enabled; + QSound * engine; QStringMap availableThemes; protected: void ensureThemeDirectoryExists();