-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoundeffectmedia.cpp
More file actions
71 lines (63 loc) · 2.3 KB
/
soundeffectmedia.cpp
File metadata and controls
71 lines (63 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "soundeffectmedia.h"
#include "qaudiodevice.h"
#include <QAudioOutput>
#include <QAudioDevice>
#include <QSoundEffect>
SoundEffectMedia::SoundEffectMedia(QAudioOutput *out, QObject *parent)
: QObject{parent}
, m_volume(1.0f)
, m_effect(new QSoundEffect(out->device(), this))
, m_decoder(new QAudioDecoder(this))
{
connect(this, &SoundEffectMedia::volumeChanged,
m_effect, &QSoundEffect::setVolume);
m_effect->setLoopCount(1);
m_effect->setVolume(1.0f);
// Fire any pending play request once the sound finishes loading.
connect(m_effect, &QSoundEffect::statusChanged, this, [this]() {
if (m_effect->status() == QSoundEffect::Ready && m_pendingPlay) {
m_pendingPlay = false;
m_effect->play();
}
});
// Connecting the audio decoder signals/slots
using MFP = void(QAudioDecoder::*)(QAudioDecoder::Error);
connect(m_decoder, MFP(&QAudioDecoder::error),
[=](QAudioDecoder::Error error) {
qDebug() << "Received error " << error<< m_decoder->errorString() << "for path:" << this->resourcePath();
emit decodeError(error);
// Emit ready(0) so callers can still build animation groups even when
// the decoder fails; sound will fire without an intentional delay.
emit ready(0);
});
connect(m_decoder, &QAudioDecoder::durationChanged,
this, &SoundEffectMedia::ready);
}
void SoundEffectMedia::play() {
if (m_effect->status() == QSoundEffect::Ready) {
m_effect->play();
} else {
// Sound hasn't finished loading yet; fire it once it becomes ready.
m_pendingPlay = true;
}
}
qint64 SoundEffectMedia::duration() const {
return m_decoder->duration();
}
QString SoundEffectMedia::resourcePath() const {
return m_effect->source().toString();
}
QString SoundEffectMedia::localPath() const {
return m_decoder->source().toString();
}
void SoundEffectMedia::initialize(QString fsPath, QString resourcePath) {
m_effect->setSource(QUrl(resourcePath));
assert(!m_effect->isLoaded());
assert(!m_effect->isMuted());
m_decoder->setSource(QUrl::fromLocalFile(fsPath));
QAudioFormat format;
format.setSampleRate(48000);
format.setChannelCount(1);
m_decoder->setAudioFormat(format);
m_decoder->start();
}