soundplayer.cpp
2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#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");
}