#include "soundplayer.h" #include "system.h" namespace { QThread playThread; } SoundPlayWorker::SoundPlayWorker() { current = 0; click = 0; } void SoundPlayWorker::play(const QString &filename) { if (current && !current->isFinished()) current->stop(); if (click && click->isPlaying()) click->stop(); if (map.contains(filename)) current = map.value(filename); else { current = new QSound(filename); map[filename] = current; } System::setVolume(70); current->play(); } void SoundPlayWorker::playClick() { if (current && !current->isFinished()) current->stop(); if (click && click->isPlaying()) click->stop(); if (click == 0) { click = new QSoundEffect; click->setSource(QUrl::fromLocalFile(":/sounds/button.wav")); click->setVolume(1.0); } System::setVolume(80); click->play(); } SoundPlayer *SoundPlayer::instance = 0; SoundPlayer::SoundPlayer() { instance = this; SoundPlayWorker *w = new SoundPlayWorker; w->moveToThread(&playThread); connect(this, SIGNAL(operate(QString)), w, SLOT(play(QString))); connect(this, SIGNAL(click()), w, SLOT(playClick())); playThread.start(); } void SoundPlayer::play(const QString &filename) { emit operate(filename); } void SoundPlayer::emitClick() { emit click(); } void SoundPlayer::playClick() { if (instance == 0) instance = new SoundPlayer; instance->emitClick(); } void SoundPlayer::playStart() { if (instance == 0) instance = new SoundPlayer; instance->play(":/sounds/start.wav"); } void SoundPlayer::playStop() { if (instance == 0) instance = new SoundPlayer; instance->play(":/sounds/stop.wav"); } void SoundPlayer::playError1() { if (instance == 0) instance = new SoundPlayer; instance->play(":/sounds/error1.wav"); } void SoundPlayer::playError2() { if (instance == 0) instance = new SoundPlayer; instance->play(":/sounds/error2.wav"); }