don't call QAudioOutput constructor unless sound is enabled

This commit is contained in:
Max-Wilhelm Bruker 2012-04-22 12:51:33 +02:00
parent 81d7c08f08
commit 8f6de6637f
4 changed files with 28 additions and 11 deletions

View file

@ -191,6 +191,7 @@ void SettingsCache::setSoundEnabled(int _soundEnabled)
{ {
soundEnabled = _soundEnabled; soundEnabled = _soundEnabled;
settings->setValue("sound/enabled", soundEnabled); settings->setValue("sound/enabled", soundEnabled);
emit soundEnabledChanged();
} }
void SettingsCache::setSoundPath(const QString &_soundPath) void SettingsCache::setSoundPath(const QString &_soundPath)

View file

@ -22,6 +22,7 @@ signals:
void horizontalHandChanged(); void horizontalHandChanged();
void invertVerticalCoordinateChanged(); void invertVerticalCoordinateChanged();
void minPlayersForMultiColumnLayoutChanged(); void minPlayersForMultiColumnLayoutChanged();
void soundEnabledChanged();
void soundPathChanged(); void soundPathChanged();
void ignoreUnregisteredUsersChanged(); void ignoreUnregisteredUsersChanged();
private: private:

View file

@ -6,20 +6,14 @@
#include <QBuffer> #include <QBuffer>
SoundEngine::SoundEngine(QObject *parent) SoundEngine::SoundEngine(QObject *parent)
: QObject(parent) : QObject(parent), audio(0)
{ {
inputBuffer = new QBuffer; inputBuffer = new QBuffer(this);
QAudioFormat format;
format.setFrequency(44100);
format.setChannels(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
audio = new QAudioOutput(format, this);
connect(settingsCache, SIGNAL(soundPathChanged()), this, SLOT(cacheData())); connect(settingsCache, SIGNAL(soundPathChanged()), this, SLOT(cacheData()));
connect(settingsCache, SIGNAL(soundEnabledChanged()), this, SLOT(soundEnabledChanged()));
cacheData(); cacheData();
soundEnabledChanged();
} }
void SoundEngine::cacheData() void SoundEngine::cacheData()
@ -34,9 +28,29 @@ void SoundEngine::cacheData()
} }
} }
void SoundEngine::soundEnabledChanged()
{
if (settingsCache->getSoundEnabled()) {
qDebug("SoundEngine: enabling sound");
QAudioFormat format;
format.setFrequency(44100);
format.setChannels(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
audio = new QAudioOutput(format, this);
} else if (audio) {
qDebug("SoundEngine: disabling sound");
audio->stop();
audio->deleteLater();
audio = 0;
}
}
void SoundEngine::playSound(const QString &fileName) void SoundEngine::playSound(const QString &fileName)
{ {
if (!settingsCache->getSoundEnabled()) if (!audio)
return; return;
audio->stop(); audio->stop();

View file

@ -16,6 +16,7 @@ private:
QAudioOutput *audio; QAudioOutput *audio;
private slots: private slots:
void cacheData(); void cacheData();
void soundEnabledChanged();
public: public:
SoundEngine(QObject *parent = 0); SoundEngine(QObject *parent = 0);
public slots: public slots: