7#include "audioplayer.h"
10#include <KLocalizedString>
14#include <QRandomGenerator>
16#include "config-okular.h"
19#include <phonon/abstractmediastream.h>
20#include <phonon/audiooutput.h>
21#include <phonon/mediaobject.h>
22#include <phonon/path.h>
41class AudioPlayerPrivate
44 explicit AudioPlayerPrivate(AudioPlayer *qq);
46 ~AudioPlayerPrivate();
49 bool play(
const SoundInfo &si);
56 QHash<int, PlayData *> m_playing;
57 QUrl m_currentDocument;
66 explicit SoundInfo(
const Sound *s =
nullptr,
const SoundAction *ls =
nullptr)
75 synchronous = ls->synchronous();
92 : m_mediaobject(nullptr)
103 m_mediaobject->play();
108 m_mediaobject->stop();
109 delete m_mediaobject;
114 PlayData(
const PlayData &) =
delete;
115 PlayData &operator=(
const PlayData &) =
delete;
117 Phonon::MediaObject *m_mediaobject;
118 Phonon::AudioOutput *m_output;
123AudioPlayerPrivate::AudioPlayerPrivate(
AudioPlayer *qq)
129AudioPlayerPrivate::~AudioPlayerPrivate()
134int AudioPlayerPrivate::newId()
const
138 QHash<int, PlayData *>::const_iterator it;
139 QHash<int, PlayData *>::const_iterator itEnd = m_playing.constEnd();
141 newid = random->bounded(RAND_MAX);
142 it = m_playing.constFind(newid);
143 }
while (it != itEnd);
147bool AudioPlayerPrivate::play(
const SoundInfo &si)
149 qCDebug(OkularCoreDebug);
150 PlayData *data =
new PlayData();
151 data->m_output =
new Phonon::AudioOutput(Phonon::NotificationCategory);
152 data->m_output->setVolume(si.volume);
153 data->m_mediaobject =
new Phonon::MediaObject();
154 Phonon::createPath(data->m_mediaobject, data->m_output);
158 switch (si.sound->soundType()) {
160 QString url = si.sound->url();
161 qCDebug(OkularCoreDebug) <<
"External," << url;
166 data->m_mediaobject->setCurrentSource(newurl);
167 m_playing.insert(newid, data);
173 QByteArray filedata = si.sound->data();
174 qCDebug(OkularCoreDebug) <<
"Embedded," << filedata.
length();
176 qCDebug(OkularCoreDebug) <<
"Mediaobject:" << data->m_mediaobject;
179 data->m_buffer =
new QBuffer();
180 data->m_buffer->setData(filedata);
181 data->m_mediaobject->setCurrentSource(Phonon::MediaSource(data->m_buffer));
182 m_playing.insert(newid, data);
193 qCDebug(OkularCoreDebug) <<
"PLAY";
200void AudioPlayerPrivate::stopPlayings()
202 qDeleteAll(m_playing);
207void AudioPlayerPrivate::finished(
int id)
209 QHash<int, PlayData *>::iterator it = m_playing.find(
id);
210 if (it == m_playing.end()) {
214 SoundInfo si = it.value()->m_info;
224 qCDebug(OkularCoreDebug) <<
"finished," << m_playing.count();
227AudioPlayer::AudioPlayer()
229 , d(new AudioPlayerPrivate(this))
233AudioPlayer::~AudioPlayer()
240 static AudioPlayer ap;
256 qCDebug(OkularCoreDebug);
257 SoundInfo si(sound, linksound);
278void AudioPlayer::resetDocument()
280 d->m_currentDocument = {};
283void AudioPlayer::setDocument(
const QUrl &url, Okular::Document *document)
286 d->m_currentDocument = url;
293class AudioPlayerPrivate
300AudioPlayer::AudioPlayer()
301 : d(new AudioPlayerPrivate())
307 static AudioPlayer ap;
315 Q_EMIT d->document->warning(
i18n(
"This Okular is built without audio support"), 2000);
327AudioPlayer::~AudioPlayer() noexcept
331void AudioPlayer::resetDocument()
333 d->document =
nullptr;
336void AudioPlayer::setDocument(
const QUrl &url, Okular::Document *document)
339 d->document = document;
344#include "moc_audioplayer.cpp"
void playSound(const Sound *sound, const SoundAction *linksound=nullptr)
Enqueue the specified sound for playing, optionally taking more information about the playing from th...
void stopPlaybacks()
Tell the AudioPlayer to stop all the playbacks.
State state() const
Return state of sound (playing/stopped)
static AudioPlayer * instance()
Gets the instance of the audio player.
State
The state of AudioPlayer.
@ StoppedState
The AudioPlayer isn't playing a audio file.
@ PlayingState
The AudioPlayer is playing a audio file.
The Sound action plays a sound on activation.
Contains information about a sound object.
SoundType soundType() const
Returns the type of the sound object.
@ Embedded
Is stored embedded in the document.
@ External
Is stored at external resource (e.g. url)
QString i18n(const char *text, const TYPE &arg...)
KGUIADDONS_EXPORT QColor mix(const QColor &c1, const QColor &c2, qreal bias=0.5)
QAction * repeat(const QObject *recvr, const char *slot, QObject *parent)
bool isEmpty() const const
qsizetype length() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QRandomGenerator * global()
bool isEmpty() const const