17#include "soundplayer.h"
18#include <QAudioOutput>
19#include <QMediaPlayer>
25SoundPlayer::SoundPlayer()
29SoundPlayer::~SoundPlayer()
39 Q_ASSERT(clip !=
nullptr);
42 mMediaPlayer =
new QMediaPlayer(
this);
44 QFile file(clip->fileName());
47 mBuffer.
setData(file.readAll());
50#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
51 mMediaPlayer->setAudioOutput(
new QAudioOutput(
this));
58 clip->attachPlayer(
this);
61void SoundPlayer::onKeyFrameDestroy(
KeyFrame* keyFrame)
66bool SoundPlayer::isValid()
70 return (mMediaPlayer->error() == QMediaPlayer::NoError);
75void SoundPlayer::play()
83void SoundPlayer::pause()
87 mMediaPlayer->pause();
91void SoundPlayer::stop()
99int64_t SoundPlayer::duration()
103 return mMediaPlayer->duration();
108void SoundPlayer::setMediaPlayerPosition(qint64 pos)
112 mMediaPlayer->setPosition(pos);
116void SoundPlayer::makeConnections()
118#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
119 connect(mMediaPlayer, &QMediaPlayer::errorOccurred,
this, [](QMediaPlayer::Error err,
const QString&)
121 auto errorSignal =
static_cast<void (QMediaPlayer::*)(QMediaPlayer::Error)
>(&QMediaPlayer::error);
122 connect(mMediaPlayer, errorSignal,
this, [](QMediaPlayer::Error err)
125 qDebug() <<
"MediaPlayer Error: " << err;
128 connect(mMediaPlayer, &QMediaPlayer::durationChanged, [
this](qint64 duration)
130 qDebug() <<
"MediaPlayer durationChanged :" << duration;
131 emit durationChanged(
this, duration);
virtual bool open(QIODevice::OpenMode flags) override
void setData(const QByteArray &data)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QUrl fromLocalFile(const QString &localFile)