Avoid multiple sounds playing at the same time

This commit is contained in:
Fabio Bas 2015-09-13 17:22:58 +02:00
parent 85aa866e02
commit f78c01fa88
2 changed files with 28 additions and 3 deletions

View file

@ -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)
themeChangedSlot(); themeChangedSlot();
} }
SoundEngine::~SoundEngine()
{
if(engine)
{
delete engine;
engine = 0;
}
}
void SoundEngine::soundEnabledChanged() void SoundEngine::soundEnabledChanged()
{ {
if (settingsCache->getSoundEnabled()) { if (settingsCache->getSoundEnabled()) {
@ -57,7 +66,7 @@ void SoundEngine::soundEnabledChanged()
} }
#else #else
qDebug("SoundEngine: enabling sound"); qDebug("SoundEngine: enabling sound");
enabled = true; enabled = true;
#endif #endif
} else { } else {
qDebug("SoundEngine: disabling sound"); qDebug("SoundEngine: disabling sound");
@ -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()

View file

@ -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();