diff options
Diffstat (limited to 'pacman-c++/audioplayer.cpp')
| -rw-r--r-- | pacman-c++/audioplayer.cpp | 97 |
1 files changed, 89 insertions, 8 deletions
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 | } | ||
