From 507f8b6ca5679c4898b721520dd4011f6b9e5824 Mon Sep 17 00:00:00 2001 From: manuel Date: Fri, 8 Apr 2011 15:18:38 +0200 Subject: make audioplayer useful --- pacman-c++/audioplayer.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 8 deletions(-) (limited to 'pacman-c++/audioplayer.cpp') diff --git a/pacman-c++/audioplayer.cpp b/pacman-c++/audioplayer.cpp index 8620190..32988e6 100644 --- a/pacman-c++/audioplayer.cpp +++ b/pacman-c++/audioplayer.cpp @@ -1,21 +1,47 @@ #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(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); - m_output = new Phonon::AudioOutput(Phonon::MusicCategory, this); + m_output = new Phonon::AudioOutput(Phonon::MusicCategory, m_player); Phonon::createPath(m_player, m_output); + + preload(); + test(); } AudioPlayer *AudioPlayer::self() @@ -35,21 +61,76 @@ void AudioPlayer::setMuted(bool mute) m_output->setMuted(mute); } -void AudioPlayer::playIntro() +void AudioPlayer::play(AudioPlayer::Sound sound) { + if (!m_working) + emit finished_p(); + m_player->stop(); - m_player->setCurrentSource(new QFile(":/sound/intro")); + 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])); +} + +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() { - qDebug() << "finished"; emit finished(); } -void AudioPlayer::stateChanged_p(Phonon::State newstate, Phonon::State oldstate) +void AudioPlayer::stateChanged_p(Phonon::State newstate, Phonon::State /* oldstate */) { - qDebug() << "old=" << oldstate << "new=" << newstate; + 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))); +} -- cgit v1.2.3