diff options
| author | manuel <manuel@mausz.at> | 2011-05-10 22:28:58 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-05-10 22:28:58 +0200 |
| commit | 1a6c940ed9d7f6136da0e13148314072665342c5 (patch) | |
| tree | 467b259d966ea1ddf9f66440066f57cf3eed68c4 /pacman-c++/phononplayer/phononplayer.cpp | |
| parent | cc1bb779661217171418adb0ddbd1ce01815463b (diff) | |
| download | foop-1a6c940ed9d7f6136da0e13148314072665342c5.tar.gz foop-1a6c940ed9d7f6136da0e13148314072665342c5.tar.bz2 foop-1a6c940ed9d7f6136da0e13148314072665342c5.zip | |
- refactorized audio once more: audio is now a plugin which gets loaded at runtime
- thus server has no dependency to phonon any more
- remove client dependency to qtnetwork
- fix enet deinitialization on windows
Diffstat (limited to 'pacman-c++/phononplayer/phononplayer.cpp')
| -rw-r--r-- | pacman-c++/phononplayer/phononplayer.cpp | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/pacman-c++/phononplayer/phononplayer.cpp b/pacman-c++/phononplayer/phononplayer.cpp new file mode 100644 index 0000000..1f537d6 --- /dev/null +++ b/pacman-c++/phononplayer/phononplayer.cpp | |||
| @@ -0,0 +1,169 @@ | |||
| 1 | #include "phononplayer.h" | ||
| 2 | #include <QtPlugin> | ||
| 3 | #include <QCoreApplication> | ||
| 4 | |||
| 5 | AudioPlayer *PhononPlayerFactory::create(QObject *parent) | ||
| 6 | { | ||
| 7 | return new PhononPlayer(parent); | ||
| 8 | } | ||
| 9 | |||
| 10 | Q_EXPORT_PLUGIN2(phononplayer, PhononPlayerFactory) | ||
| 11 | |||
| 12 | /* --------------------------------------------------------------- */ | ||
| 13 | |||
| 14 | PhononPlayer::PhononPlayer(QObject *parent) | ||
| 15 | : AudioPlayer(parent) | ||
| 16 | { | ||
| 17 | m_working = AudioManager::isWorking(); | ||
| 18 | m_player = new Phonon::MediaObject(this); | ||
| 19 | m_output = new Phonon::AudioOutput(Phonon::MusicCategory, this); | ||
| 20 | Phonon::createPath(m_player, m_output); | ||
| 21 | } | ||
| 22 | |||
| 23 | bool PhononPlayer::isWorking() const | ||
| 24 | { | ||
| 25 | return m_working; | ||
| 26 | } | ||
| 27 | |||
| 28 | void PhononPlayer::setMuted(bool mute) | ||
| 29 | { | ||
| 30 | if (!isWorking()) | ||
| 31 | return; | ||
| 32 | m_output->setMuted(mute); | ||
| 33 | } | ||
| 34 | |||
| 35 | bool PhononPlayer::isMuted() const | ||
| 36 | { | ||
| 37 | return m_output->isMuted(); | ||
| 38 | } | ||
| 39 | |||
| 40 | void PhononPlayer::play() | ||
| 41 | { | ||
| 42 | m_player->play(); | ||
| 43 | } | ||
| 44 | |||
| 45 | void PhononPlayer::play(Sound::Type sound) | ||
| 46 | { | ||
| 47 | if (!m_working) | ||
| 48 | return; | ||
| 49 | m_player->setCurrentSource(Phonon::MediaSource(AudioManager::self()->sound(sound))); | ||
| 50 | play(); | ||
| 51 | } | ||
| 52 | |||
| 53 | bool PhononPlayer::isPlaying() | ||
| 54 | { | ||
| 55 | return m_player->state() == Phonon::PlayingState; | ||
| 56 | } | ||
| 57 | |||
| 58 | void PhononPlayer::enqueue(Sound::Type sound) | ||
| 59 | { | ||
| 60 | m_player->enqueue(Phonon::MediaSource(AudioManager::self()->sound(sound))); | ||
| 61 | } | ||
| 62 | |||
| 63 | void PhononPlayer::pause() | ||
| 64 | { | ||
| 65 | m_player->pause(); | ||
| 66 | } | ||
| 67 | |||
| 68 | bool PhononPlayer::isPaused() | ||
| 69 | { | ||
| 70 | return m_player->state() == Phonon::PausedState; | ||
| 71 | } | ||
| 72 | |||
| 73 | void PhononPlayer::stop() | ||
| 74 | { | ||
| 75 | m_player->stop(); | ||
| 76 | } | ||
| 77 | |||
| 78 | bool PhononPlayer::isStopped() | ||
| 79 | { | ||
| 80 | return m_player->state() == Phonon::StoppedState; | ||
| 81 | } | ||
| 82 | |||
| 83 | void PhononPlayer::clear() | ||
| 84 | { | ||
| 85 | m_player->clear(); | ||
| 86 | } | ||
| 87 | |||
| 88 | void PhononPlayer::clearQueue() | ||
| 89 | { | ||
| 90 | m_player->clearQueue(); | ||
| 91 | } | ||
| 92 | |||
| 93 | void PhononPlayer::setPrefinishMark(qint32 msecToEnd) | ||
| 94 | { | ||
| 95 | connect(m_player, SIGNAL(prefinishMarkReached(qint32)), this, SLOT(prefinishMarkReached_ex(qint32)), Qt::UniqueConnection); | ||
| 96 | m_player->setPrefinishMark(msecToEnd); | ||
| 97 | } | ||
| 98 | |||
| 99 | /* this is a simple hack to check if phonon can actually play sounds.. */ | ||
| 100 | void PhononPlayer::test(QFile *testsound, qint32 length) | ||
| 101 | { | ||
| 102 | m_player->stop(); | ||
| 103 | m_output->setVolume(0); | ||
| 104 | m_player->setCurrentSource(Phonon::MediaSource(testsound)); | ||
| 105 | connect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_ex(Phonon::State, Phonon::State))); | ||
| 106 | m_player->play(); | ||
| 107 | |||
| 108 | QTimer timer; | ||
| 109 | timer.setSingleShot(true); | ||
| 110 | connect(&timer, SIGNAL(timeout()), this, SLOT(testFinished())); | ||
| 111 | timer.start(length); | ||
| 112 | while(timer.isActive()) | ||
| 113 | { | ||
| 114 | qApp->processEvents(); | ||
| 115 | Sleeper::msleep(1); | ||
| 116 | } | ||
| 117 | clear(); | ||
| 118 | } | ||
| 119 | |||
| 120 | void PhononPlayer::prefinishMarkReached_ex(qint32 mark) | ||
| 121 | { | ||
| 122 | emit prefinishMarkReached(mark); | ||
| 123 | } | ||
| 124 | |||
| 125 | void PhononPlayer::finished_ex() | ||
| 126 | { | ||
| 127 | emit finished(); | ||
| 128 | } | ||
| 129 | |||
| 130 | void PhononPlayer::stateChanged_ex(Phonon::State newstate, Phonon::State oldstate) | ||
| 131 | { | ||
| 132 | if (newstate == Phonon::PlayingState) | ||
| 133 | m_player->pause(); | ||
| 134 | if (oldstate == Phonon::PlayingState && newstate == Phonon::PausedState) | ||
| 135 | m_player->stop(); | ||
| 136 | else if (oldstate == Phonon::PausedState && newstate == Phonon::StoppedState) | ||
| 137 | { | ||
| 138 | m_working = true; | ||
| 139 | m_player->stop(); | ||
| 140 | m_output->setVolume(1); | ||
| 141 | qDebug() << "Sound is working for you!"; | ||
| 142 | disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_ex(Phonon::State, Phonon::State))); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | void PhononPlayer::testFinished() | ||
| 147 | { | ||
| 148 | if (!m_working) | ||
| 149 | qWarning() << "There's no sound for you :("; | ||
| 150 | disconnect(m_player, SIGNAL(stateChanged(Phonon::State, Phonon::State)), this, SLOT(stateChanged_ex(Phonon::State, Phonon::State))); | ||
| 151 | } | ||
| 152 | |||
| 153 | /* --------------------------------------------------------------- */ | ||
| 154 | |||
| 155 | void PhononPlayer::Sleeper::sleep(unsigned long secs) | ||
| 156 | { | ||
| 157 | QThread::sleep(secs); | ||
| 158 | } | ||
| 159 | |||
| 160 | void PhononPlayer::Sleeper::msleep(unsigned long msecs) | ||
| 161 | { | ||
| 162 | QThread::msleep(msecs); | ||
| 163 | } | ||
| 164 | |||
| 165 | void PhononPlayer::Sleeper::usleep(unsigned long usecs) | ||
| 166 | { | ||
| 167 | QThread::usleep(usecs); | ||
| 168 | } | ||
| 169 | |||
