#include "soundplayer.h" namespace { QThread playThread; } SoundPlayWorker::SoundPlayWorker() { current = 0; click = 0; } void SoundPlayWorker::play(const QString &filename) { if (current && !current->isFinished()) current->stop(); if (map.contains(filename)) current = map.value(filename); else { current = new QSound(filename); map[filename] = current; } current->play(); } void SoundPlayWorker::playClick() { if (click == 0) { click = new QSoundEffect; click->setSource(QUrl::fromLocalFile(":/sounds/button.wav")); click->setVolume(1.0); } 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(); }z 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"); }