Improve the windows respond by using thread
[QFreeRecite.git] / src / gui / Speaker.cpp
blobd53909a17aca0fc9d0dfba9f548201c7e04c2372
1 #include <QProcess>
2 #include <QString>
3 #include <QStringList>
4 #include <QSettings>
5 #include <QFileInfo>
6 #include <Debug.h>
7 #include <ConfigHolder.h>
8 #include "Speaker.h"
10 #ifdef WIN32
11 #include <QApplication>
12 #include "WinSpeaker.h"
13 WinSpeaker winSpeaker;
14 #endif //WIN32
16 using freeRecite::configHolder;
18 namespace Speaker {
20 QSettings settings("QFreeRecite");
22 void speak(const QString &word) {
23 QFileInfo finfo((settings.value("PronouncePath").toString().toStdString()
24 + word.toStdString()[0] + "/"
25 + word.toStdString() + ".wav").c_str());
26 if(!finfo.exists())
27 return;
28 #ifdef WIN32
29 winSpeaker.speak(finfo.absoluteFilePath().toStdString());
30 #else
31 QProcess::startDetached("aplay",
32 QStringList(finfo.absoluteFilePath()));
33 #endif
36 void play(const QString &sound) {
37 if(!settings.value("SoundEffectOpen").toBool())
38 return;
39 std::string fileName = configHolder.musicDir()
40 + sound.toStdString() + ".wav";
41 #ifdef WIN32
42 //If thread can't play it, then paly in a new process!
43 if(!winSpeaker.speak(fileName) &&
44 (sound == "type" || sound == "ok" || sound == "fail") ) {
45 QProcess::startDetached("aplay",
46 QStringList(fileName.c_str()));
47 qApp->processEvents();
49 #else
50 QProcess::startDetached("aplay",
51 QStringList(fileName.c_str()));
52 #endif
55 } //namespace Speaker