Blame view

samplecode/playclicker/soundplayer.cpp 2.84 KB
04299ad6f   김태훈   테스트 프로그램 작성
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  #include "soundplayer.h"
  #include "unistd.h"
  
  namespace {
  QThread playThread;
  }
  
  SoundPlayWorker::SoundPlayWorker()
  {
      repeat_ = false;
      proc = new QProcess(this);
  
      connect(proc, SIGNAL(finished(int)), SLOT(onFinished()));
  }
  
  void SoundPlayWorker::play(const QString &filename)
  {
      if (proc->state() != QProcess::NotRunning)
      {
          proc->terminate();
          proc->waitForFinished();
      }
  
      proc->start(QString("aplay"), QStringList(filename));
      proc->waitForStarted();
  }
  
  void SoundPlayWorker::playClick()
  {
      play("/falinux/sounds/button.wav");
  }
  
  void SoundPlayWorker::setVolume(int volume)
  {
  }
  
  void SoundPlayWorker::repeat(const QString &filename)
  {
      repeat_ = true;
      play(filename);
  }
  
  void SoundPlayWorker::stop()
  {
      repeat_ = false;
      if (proc->state() != QProcess::NotRunning)
      {
          proc->terminate();
          proc->waitForFinished();
      }
  }
  
  void SoundPlayWorker::onFinished()
  {
      if (repeat_)
          proc->start();
  }
  
  SoundPlayer *SoundPlayer::instance = 0;
  SoundPlayer::SoundPlayer()
  {
      instance = this;
  
      SoundPlayWorker *w = new SoundPlayWorker;
      w->moveToThread(&playThread);
      connect(this, SIGNAL(setVolume(int)), w, SLOT(setVolume(int)));
      connect(this, SIGNAL(operate(QString)), w, SLOT(play(QString)));
      connect(this, SIGNAL(click()), w, SLOT(playClick()));
      connect(this, SIGNAL(operateRepeat(QString)), w, SLOT(repeat(QString)));
      connect(this, SIGNAL(operateStop()), w, SLOT(stop()));
  
      playThread.start();
  }
  
  void SoundPlayer::play(const QString &filename)
  {
  
      emit operate(filename);
  }
  
  void SoundPlayer::emitClick()
  {
  
      emit click();
  }
  
  void SoundPlayer::repeat(const QString &filename)
  {
  
      emit operateRepeat(filename);
  }
  
  void SoundPlayer::stopPlay()
  {
      emit operateStop();
  }
  
  void SoundPlayer::playClick()
  {
      if (instance == 0)
          instance = new SoundPlayer;
  
      instance->emitClick();
  }
  
  void SoundPlayer::playStart()
  {
      if (instance == 0)
          instance = new SoundPlayer;
  
      instance->play("/falinux/sounds/start.wav");
  }
  
  void SoundPlayer::playStop()
  {
      if (instance == 0)
          instance = new SoundPlayer;
  
      instance->play("/falinux/sounds/stop.wav");
  }
  
  void SoundPlayer::playError1()
  {
      if (instance == 0)
          instance = new SoundPlayer;
  
      instance->play("/falinux/sounds/error1.wav");
  }
  
  void SoundPlayer::playError2()
  {
      if (instance == 0)
          instance = new SoundPlayer;
  
      instance->play("/falinux/sounds/error2.wav");
  }
  
  void SoundPlayer::repeatError1()
  {
      if (instance == 0)
          instance = new SoundPlayer;
  
      instance->repeat("/falinux/sounds/error1.wav");
  }
  
  void SoundPlayer::repeatError2()
  {
      if (instance == 0)
          instance = new SoundPlayer;
  
      instance->repeat("/falinux/sounds/error2.wav");
  }
  
  void SoundPlayer::stop()
  {
      if (instance == 0)
          instance = new SoundPlayer;
  
      instance->stopPlay();
  }