#include "audioplayer.h" #include #include #include #include #include #include #include #include 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(); #ifndef SERVER test(); #endif // SERVER } AudioPlayer *AudioPlayer::self() { if (!m_instance) m_instance = new AudioPlayer(); return m_instance; } bool AudioPlayer::isWorking() const { return m_working; } void AudioPlayer::stop() { m_player->stop(); } void AudioPlayer::setMuted(bool mute) { m_output->setMuted(mute); } bool AudioPlayer::isMuted() const { return m_output->isMuted(); } void AudioPlayer::play(AudioPlayer::Sound sound) { #ifdef SERVER return; #endif // SERVER if (!m_working) emit finished_p(); m_player->stop(); m_player->setCurrentSource(Phonon::MediaSource(m_sounds[sound])); m_player->play(); } void AudioPlayer::clear() { if (!m_working) return; m_player->clear(); } void AudioPlayer::enqueue(AudioPlayer::Sound sound) { if (!m_working) return; m_player->enqueue(Phonon::MediaSource(m_sounds[sound])); } void AudioPlayer::clearQueue() const { 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))); }