diff options
| author | manuel <manuel@mausz.at> | 2011-04-08 15:18:38 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-04-08 15:18:38 +0200 |
| commit | 507f8b6ca5679c4898b721520dd4011f6b9e5824 (patch) | |
| tree | ba162976ed22e04586f6268db6a2e46621c1c036 | |
| parent | d1d48ba62695572a2091867b827b1304b0da08d3 (diff) | |
| download | foop-507f8b6ca5679c4898b721520dd4011f6b9e5824.tar.gz foop-507f8b6ca5679c4898b721520dd4011f6b9e5824.tar.bz2 foop-507f8b6ca5679c4898b721520dd4011f6b9e5824.zip | |
make audioplayer useful
| -rw-r--r-- | pacman-c++/actor.cpp | 28 | ||||
| -rw-r--r-- | pacman-c++/actor.h | 4 | ||||
| -rw-r--r-- | pacman-c++/audioplayer.cpp | 97 | ||||
| -rw-r--r-- | pacman-c++/audioplayer.h | 22 | ||||
| -rw-r--r-- | pacman-c++/client.cpp | 1 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 6 | ||||
| -rw-r--r-- | pacman-c++/pacman.qrc | 8 |
7 files changed, 123 insertions, 43 deletions
diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index 7358756..2b65528 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | #include "actor.h" | 1 | #include "actor.h" |
| 2 | #include "animationmanager.h" | 2 | #include "animationmanager.h" |
| 3 | #include "audioplayer.h" | ||
| 3 | #include <QtCore/QPropertyAnimation> | 4 | #include <QtCore/QPropertyAnimation> |
| 4 | #include <QtCore/QVariantAnimation> | 5 | #include <QtCore/QVariantAnimation> |
| 5 | #include <phonon/AudioOutput> | ||
| 6 | #include <QDebug> | 6 | #include <QDebug> |
| 7 | 7 | ||
| 8 | static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress) | 8 | static QVariant myBooleanInterpolator(const bool &start, const bool &end, qreal progress) |
| @@ -47,15 +47,7 @@ Actor::Actor(Color::Color color, bool local, QGraphicsItem *parent) | |||
| 47 | m_eating.append(setupEatingAnimation(Actor::Down)); | 47 | m_eating.append(setupEatingAnimation(Actor::Down)); |
| 48 | 48 | ||
| 49 | /* setup player */ | 49 | /* setup player */ |
| 50 | m_player = new Phonon::MediaObject(this); | 50 | connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(enqueue())); |
| 51 | Phonon::AudioOutput *audio_output = new Phonon::AudioOutput(Phonon::MusicCategory, this); | ||
| 52 | Phonon::createPath(m_player, audio_output); | ||
| 53 | connect(m_player, SIGNAL(finished()), this, SLOT(enqueue())); | ||
| 54 | |||
| 55 | /* preload sounds */ | ||
| 56 | m_sounds.append(new QFile(":/sound/wakawaka")); | ||
| 57 | m_sounds.append(new QFile(":/sound/die")); | ||
| 58 | m_sounds.append(new QFile(":/sound/eatingcherry")); | ||
| 59 | 51 | ||
| 60 | /* make the picture showing the current direction visible */ | 52 | /* make the picture showing the current direction visible */ |
| 61 | m_images[m_direction]->setVisible(true); | 53 | m_images[m_direction]->setVisible(true); |
| @@ -114,11 +106,9 @@ void Actor::move(Actor::Movement direction) | |||
| 114 | if (isMoving()) | 106 | if (isMoving()) |
| 115 | return; | 107 | return; |
| 116 | 108 | ||
| 117 | if (m_local && m_player->state() != Phonon::PlayingState) | 109 | if (m_local && AudioPlayer::self()->state() != Phonon::PlayingState) |
| 118 | { | 110 | { |
| 119 | m_player->stop(); | 111 | AudioPlayer::self()->play(AudioPlayer::WakaWaka); |
| 120 | m_player->setCurrentSource(m_sounds[0]); | ||
| 121 | m_player->play(); | ||
| 122 | } | 112 | } |
| 123 | 113 | ||
| 124 | /* stop current animation */ | 114 | /* stop current animation */ |
| @@ -184,23 +174,19 @@ bool Actor::isMoving() | |||
| 184 | void Actor::enqueue() | 174 | void Actor::enqueue() |
| 185 | { | 175 | { |
| 186 | if (isMoving()) | 176 | if (isMoving()) |
| 187 | m_player->enqueue(m_sounds[0]); | 177 | AudioPlayer::self()->enqueue(AudioPlayer::WakaWaka); |
| 188 | } | 178 | } |
| 189 | 179 | ||
| 190 | void Actor::die() | 180 | void Actor::die() |
| 191 | { | 181 | { |
| 192 | if (!m_local) | 182 | if (!m_local) |
| 193 | return; | 183 | return; |
| 194 | m_player->stop(); | 184 | AudioPlayer::self()->play(AudioPlayer::Die); |
| 195 | m_player->setCurrentSource(m_sounds[1]); | ||
| 196 | m_player->play(); | ||
| 197 | } | 185 | } |
| 198 | 186 | ||
| 199 | void Actor::eatingCherry() | 187 | void Actor::eatingCherry() |
| 200 | { | 188 | { |
| 201 | if (!m_local) | 189 | if (!m_local) |
| 202 | return; | 190 | return; |
| 203 | m_player->stop(); | 191 | AudioPlayer::self()->play(AudioPlayer::EatingCherry); |
| 204 | m_player->setCurrentSource(m_sounds[2]); | ||
| 205 | m_player->play(); | ||
| 206 | } | 192 | } |
diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index 36b2a49..77a5b34 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h | |||
| @@ -6,8 +6,6 @@ | |||
| 6 | #include <QtCore/QSequentialAnimationGroup> | 6 | #include <QtCore/QSequentialAnimationGroup> |
| 7 | #include <QtCore/QParallelAnimationGroup> | 7 | #include <QtCore/QParallelAnimationGroup> |
| 8 | #include <QList> | 8 | #include <QList> |
| 9 | #include <QFile> | ||
| 10 | #include <phonon/MediaObject> | ||
| 11 | 9 | ||
| 12 | class Actor | 10 | class Actor |
| 13 | : public PixmapItem | 11 | : public PixmapItem |
| @@ -48,8 +46,6 @@ private: | |||
| 48 | QList<PixmapItem *> m_images; | 46 | QList<PixmapItem *> m_images; |
| 49 | QList<QSequentialAnimationGroup *> m_eating; | 47 | QList<QSequentialAnimationGroup *> m_eating; |
| 50 | QParallelAnimationGroup *m_moving; | 48 | QParallelAnimationGroup *m_moving; |
| 51 | Phonon::MediaObject *m_player; | ||
| 52 | QList<QFile *> m_sounds; | ||
| 53 | }; | 49 | }; |
| 54 | 50 | ||
| 55 | #endif // ACTOR_H | 51 | #endif // ACTOR_H |
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 @@ | |||
| 1 | #include "audioplayer.h" | 1 | #include "audioplayer.h" |
| 2 | #include <phonon/AudioOutput> | 2 | #include <phonon/AudioOutput> |
| 3 | #include <phonon/MediaObject> | 3 | #include <phonon/MediaObject> |
| 4 | #include <QCoreApplication> | ||
| 5 | #include <QTimer> | ||
| 6 | #include <QThread> | ||
| 4 | #include <QFile> | 7 | #include <QFile> |
| 8 | #include <QDir> | ||
| 5 | #include <QDebug> | 9 | #include <QDebug> |
| 6 | 10 | ||
| 11 | class Sleeper | ||
| 12 | : public QThread | ||
| 13 | { | ||
| 14 | public: | ||
| 15 | static void sleep(unsigned long secs) | ||
| 16 | { | ||
| 17 | QThread::sleep(secs); | ||
| 18 | } | ||
| 19 | |||
| 20 | static void msleep(unsigned long msecs) | ||
| 21 | { | ||
| 22 | QThread::msleep(msecs); | ||
| 23 | } | ||
| 24 | |||
| 25 | static void usleep(unsigned long usecs) | ||
| 26 | { | ||
| 27 | QThread::usleep(usecs); | ||
| 28 | } | ||
| 29 | }; | ||
| 30 | |||
| 7 | // the universe's only audio player | 31 | // the universe's only audio player |
| 8 | AudioPlayer *AudioPlayer::m_instance = 0; | 32 | AudioPlayer *AudioPlayer::m_instance = 0; |
| 9 | 33 | ||
| 10 | AudioPlayer::AudioPlayer() | 34 | AudioPlayer::AudioPlayer() |
| 35 | : m_working(false) | ||
| 11 | { | 36 | { |
| 12 | m_player = new Phonon::MediaObject(this); | 37 | m_player = new Phonon::MediaObject(this); |
| 13 | |||
| 14 | connect(m_player, SIGNAL(finished()), this, SLOT(finished_p())); | 38 | connect(m_player, SIGNAL(finished()), this, SLOT(finished_p())); |
| 15 | connect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); | ||
| 16 | 39 | ||
| 17 | m_output = new Phonon::AudioOutput(Phonon::MusicCategory, this); | 40 | m_output = new Phonon::AudioOutput(Phonon::MusicCategory, m_player); |
| 18 | Phonon::createPath(m_player, m_output); | 41 | Phonon::createPath(m_player, m_output); |
| 42 | |||
| 43 | preload(); | ||
| 44 | test(); | ||
| 19 | } | 45 | } |
| 20 | 46 | ||
| 21 | AudioPlayer *AudioPlayer::self() | 47 | AudioPlayer *AudioPlayer::self() |
| @@ -35,21 +61,76 @@ void AudioPlayer::setMuted(bool mute) | |||
| 35 | m_output->setMuted(mute); | 61 | m_output->setMuted(mute); |
| 36 | } | 62 | } |
| 37 | 63 | ||
| 38 | void AudioPlayer::playIntro() | 64 | void AudioPlayer::play(AudioPlayer::Sound sound) |
| 39 | { | 65 | { |
| 66 | if (!m_working) | ||
| 67 | emit finished_p(); | ||
| 68 | |||
| 40 | m_player->stop(); | 69 | m_player->stop(); |
| 41 | m_player->setCurrentSource(new QFile(":/sound/intro")); | 70 | m_player->setCurrentSource(Phonon::MediaSource(m_sounds[sound])); |
| 42 | m_player->play(); | 71 | m_player->play(); |
| 43 | } | 72 | } |
| 44 | 73 | ||
| 74 | void AudioPlayer::enqueue(AudioPlayer::Sound sound) | ||
| 75 | { | ||
| 76 | if (!m_working) | ||
| 77 | return | ||
| 78 | m_player->enqueue(Phonon::MediaSource(m_sounds[sound])); | ||
| 79 | } | ||
| 80 | |||
| 81 | Phonon::State AudioPlayer::state() | ||
| 82 | { | ||
| 83 | return m_player->state(); | ||
| 84 | } | ||
| 85 | |||
| 86 | void AudioPlayer::preload() | ||
| 87 | { | ||
| 88 | m_sounds.clear(); | ||
| 89 | QDir sounds(":/sound"); | ||
| 90 | for(unsigned i = 1; i <= sounds.count(); ++i) | ||
| 91 | m_sounds.append(new QFile(QString(":/sound/sound%1").arg(i), m_player)); | ||
| 92 | } | ||
| 93 | |||
| 94 | /* this is a simple hack to check if phonon can actually play sounds.. */ | ||
| 95 | void AudioPlayer::test() | ||
| 96 | { | ||
| 97 | m_player->stop(); | ||
| 98 | connect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); | ||
| 99 | m_output->setVolume(0); | ||
| 100 | m_player->setCurrentSource(Phonon::MediaSource(m_sounds[AudioPlayer::WakaWaka])); | ||
| 101 | m_player->play(); | ||
| 102 | |||
| 103 | QTimer timer; | ||
| 104 | timer.setSingleShot(true); | ||
| 105 | connect(&timer, SIGNAL(timeout()), this, SLOT(testFinished())); | ||
| 106 | timer.start(500); | ||
| 107 | while(timer.isActive()) | ||
| 108 | { | ||
| 109 | qApp->processEvents(); | ||
| 110 | Sleeper::msleep(1); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 45 | void AudioPlayer::finished_p() | 114 | void AudioPlayer::finished_p() |
| 46 | { | 115 | { |
| 47 | qDebug() << "finished"; | ||
| 48 | emit finished(); | 116 | emit finished(); |
| 49 | } | 117 | } |
| 50 | 118 | ||
| 51 | void AudioPlayer::stateChanged_p(Phonon::State newstate, Phonon::State oldstate) | 119 | void AudioPlayer::stateChanged_p(Phonon::State newstate, Phonon::State /* oldstate */) |
| 52 | { | 120 | { |
| 53 | qDebug() << "old=" << oldstate << "new=" << newstate; | 121 | if (newstate != Phonon::ErrorState) |
| 122 | { | ||
| 123 | m_working = true; | ||
| 124 | m_output->setVolume(1); | ||
| 125 | qDebug() << "Sound is working for you!"; | ||
| 126 | } | ||
| 127 | disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); | ||
| 128 | m_player->stop(); | ||
| 54 | } | 129 | } |
| 55 | 130 | ||
| 131 | void AudioPlayer::testFinished() | ||
| 132 | { | ||
| 133 | if (!m_working) | ||
| 134 | qDebug() << "There's no sound for you :("; | ||
| 135 | disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_p(Phonon::State, Phonon::State))); | ||
| 136 | } | ||
diff --git a/pacman-c++/audioplayer.h b/pacman-c++/audioplayer.h index 3dc3cfd..dd128c5 100644 --- a/pacman-c++/audioplayer.h +++ b/pacman-c++/audioplayer.h | |||
| @@ -2,6 +2,8 @@ | |||
| 2 | #define AUDIOPLAYER_H | 2 | #define AUDIOPLAYER_H |
| 3 | 3 | ||
| 4 | #include <QObject> | 4 | #include <QObject> |
| 5 | #include <QList> | ||
| 6 | #include <QFile> | ||
| 5 | #include <phonon/phononnamespace.h> | 7 | #include <phonon/phononnamespace.h> |
| 6 | 8 | ||
| 7 | namespace Phonon | 9 | namespace Phonon |
| @@ -16,23 +18,41 @@ class AudioPlayer | |||
| 16 | Q_OBJECT | 18 | Q_OBJECT |
| 17 | 19 | ||
| 18 | public: | 20 | public: |
| 21 | enum Sound { | ||
| 22 | Intro = 0, | ||
| 23 | WakaWaka, | ||
| 24 | EatingCherry, | ||
| 25 | Die | ||
| 26 | }; | ||
| 27 | |||
| 28 | public: | ||
| 19 | AudioPlayer(); | 29 | AudioPlayer(); |
| 20 | static AudioPlayer *self(); | 30 | static AudioPlayer *self(); |
| 31 | bool isWorking(); | ||
| 21 | void stop(); | 32 | void stop(); |
| 22 | void setMuted(bool mute = true); | 33 | void setMuted(bool mute = true); |
| 23 | void playIntro(); | 34 | void play(Sound sound); |
| 35 | void enqueue(Sound sound); | ||
| 36 | Phonon::State state(); | ||
| 24 | 37 | ||
| 25 | signals: | 38 | signals: |
| 26 | void finished(); | 39 | void finished(); |
| 27 | 40 | ||
| 41 | private: | ||
| 42 | void test(); | ||
| 43 | void preload(); | ||
| 44 | |||
| 28 | private slots: | 45 | private slots: |
| 29 | void finished_p(); | 46 | void finished_p(); |
| 30 | void stateChanged_p(Phonon::State newstate, Phonon::State oldstate); | 47 | void stateChanged_p(Phonon::State newstate, Phonon::State oldstate); |
| 48 | void testFinished(); | ||
| 31 | 49 | ||
| 32 | private: | 50 | private: |
| 51 | bool m_working; | ||
| 33 | Phonon::MediaObject *m_player; | 52 | Phonon::MediaObject *m_player; |
| 34 | Phonon::AudioOutput *m_output; | 53 | Phonon::AudioOutput *m_output; |
| 35 | static AudioPlayer *m_instance; | 54 | static AudioPlayer *m_instance; |
| 55 | QList<QFile *> m_sounds; | ||
| 36 | }; | 56 | }; |
| 37 | 57 | ||
| 38 | #endif // AUDIOPLAYER_H | 58 | #endif // AUDIOPLAYER_H |
diff --git a/pacman-c++/client.cpp b/pacman-c++/client.cpp index 814e5e1..e4978f7 100644 --- a/pacman-c++/client.cpp +++ b/pacman-c++/client.cpp | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | #include "client.h" | 1 | #include "client.h" |
| 2 | #include "audioplayer.h" | ||
| 2 | 3 | ||
| 3 | Client::Client() | 4 | Client::Client() |
| 4 | { | 5 | { |
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index ddce051..b421c36 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -92,12 +92,8 @@ MainWidget::MainWidget() | |||
| 92 | createGui(); | 92 | createGui(); |
| 93 | updateMap(createDummyMap()); | 93 | updateMap(createDummyMap()); |
| 94 | 94 | ||
| 95 | #if 0 | ||
| 96 | emit startGame(); | ||
| 97 | #else | ||
| 98 | AudioPlayer::self()->playIntro(); | ||
| 99 | connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); | 95 | connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); |
| 100 | #endif | 96 | AudioPlayer::self()->play(AudioPlayer::Intro); |
| 101 | } | 97 | } |
| 102 | 98 | ||
| 103 | void MainWidget::createGui() | 99 | void MainWidget::createGui() |
diff --git a/pacman-c++/pacman.qrc b/pacman-c++/pacman.qrc index ceb032a..98b495d 100644 --- a/pacman-c++/pacman.qrc +++ b/pacman-c++/pacman.qrc | |||
| @@ -19,9 +19,9 @@ | |||
| 19 | <file alias="actor4icon">pics/actor4icon.png</file> | 19 | <file alias="actor4icon">pics/actor4icon.png</file> |
| 20 | </qresource> | 20 | </qresource> |
| 21 | <qresource prefix="/sound"> | 21 | <qresource prefix="/sound"> |
| 22 | <file alias="wakawaka">sound/waka_waka.ogg</file> | 22 | <file alias="sound2">sound/waka_waka.ogg</file> |
| 23 | <file alias="die">sound/die.ogg</file> | 23 | <file alias="sound4">sound/die.ogg</file> |
| 24 | <file alias="eatingcherry">sound/eating_cherry.ogg</file> | 24 | <file alias="sound3">sound/eating_cherry.ogg</file> |
| 25 | <file alias="intro">sound/intro.ogg</file> | 25 | <file alias="sound1">sound/intro.ogg</file> |
| 26 | </qresource> | 26 | </qresource> |
| 27 | </RCC> | 27 | </RCC> |
