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
|
#include "audioplayer.h"
#include <phonon/AudioOutput>
#include <phonon/MediaObject>
#include <QCoreApplication>
#include <QTimer>
#include <QThread>
#include <QFile>
#include <QDir>
#include <QDebug>
class Sleeper
: public QThread
{
public:
static void sleep(unsigned long secs)
{
QThread::sleep(secs);
}
static void msleep(unsigned long msecs)
{
QThread::msleep(msecs);
}
static void usleep(unsigned long usecs)
{
QThread::usleep(usecs);
}
};
// the universe's only audio player
AudioPlayer *AudioPlayer::m_instance = 0;
AudioPlayer::AudioPlayer()
: m_working(false)
{
m_player = new Phonon::MediaObject(this);
connect(m_player, SIGNAL(finished()), this, SLOT(finished_p()));
connect(m_player, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish_p()));
m_output = new Phonon::AudioOutput(Phonon::MusicCategory, m_player);
Phonon::createPath(m_player, m_output);
preload();
test();
}
AudioPlayer *AudioPlayer::self()
{
if (!m_instance)
m_instance = new AudioPlayer();
return m_instance;
}
void AudioPlayer::stop()
{
m_player->stop();
}
void AudioPlayer::setMuted(bool mute)
{
m_output->setMuted(mute);
}
void AudioPlayer::play(AudioPlayer::Sound sound)
{
if (!m_working)
emit finished_p();
m_player->stop();
m_player->setCurrentSource(Phonon::MediaSource(m_sounds[sound]));
m_player->play();
}
void AudioPlayer::enqueue(AudioPlayer::Sound sound)
{
if (!m_working)
return;
m_player->enqueue(Phonon::MediaSource(m_sounds[sound]));
}
void AudioPlayer::clearQueue()
{
if (!m_working)
return;
m_player->clearQueue();
}
Phonon::State AudioPlayer::state()
{
return m_player->state();
}
void AudioPlayer::preload()
{
m_sounds.clear();
QDir sounds(":/sound");
for(unsigned i = 1; i <= sounds.count(); ++i)
m_sounds.append(new QFile(QString(":/sound/sound%1").arg(i), m_player));
}
/* this is a simple hack to check if phonon can actually play sounds.. */
void AudioPlayer::test()
{
m_player->stop();
connect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State)));
m_output->setVolume(0);
m_player->setCurrentSource(Phonon::MediaSource(m_sounds[AudioPlayer::WakaWaka]));
m_player->play();
QTimer timer;
timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), this, SLOT(testFinished()));
timer.start(500);
while(timer.isActive())
{
qApp->processEvents();
Sleeper::msleep(1);
}
}
void AudioPlayer::finished_p()
{
emit finished();
}
void AudioPlayer::aboutToFinish_p()
{
emit aboutToFinish();
}
void AudioPlayer::stateChanged_p(Phonon::State newstate, Phonon::State /* oldstate */)
{
if (newstate != Phonon::ErrorState)
{
m_working = true;
m_output->setVolume(1);
qDebug() << "Sound is working for you!";
}
disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State)));
m_player->stop();
}
void AudioPlayer::testFinished()
{
if (!m_working)
qDebug() << "There's no sound for you :(";
disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State)));
}
|