17#include "soundplayer.h"
18#include <QAudioOutput>
19#include <QMediaPlayer>
24SoundPlayer::SoundPlayer()
28SoundPlayer::~SoundPlayer()
38 Q_ASSERT(clip !=
nullptr);
41 mMediaPlayer =
new QMediaPlayer(
this);
43 QFile file(clip->fileName());
46 mBuffer.
setData(file.readAll());
49#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
50 mMediaPlayer->setAudioOutput(
new QAudioOutput(
this));
57 clip->attachPlayer(
this);
60void SoundPlayer::onKeyFrameDestroy(
KeyFrame* keyFrame)
65bool SoundPlayer::isValid()
69 return (mMediaPlayer->error() == QMediaPlayer::NoError);
74void SoundPlayer::play()
82void SoundPlayer::pause()
86 mMediaPlayer->pause();
90void SoundPlayer::stop()
98int64_t SoundPlayer::duration()
102 return mMediaPlayer->duration();
107void SoundPlayer::setMediaPlayerPosition(qint64 pos)
111 mMediaPlayer->setPosition(pos);
115void SoundPlayer::makeConnections()
117#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
118 connect(mMediaPlayer, &QMediaPlayer::errorOccurred,
this, [](QMediaPlayer::Error err,
const QString&)
120 auto errorSignal =
static_cast<void (QMediaPlayer::*)(QMediaPlayer::Error)
>(&QMediaPlayer::error);
121 connect(mMediaPlayer, errorSignal,
this, [](QMediaPlayer::Error err)
124 qDebug() <<
"MediaPlayer Error: " << err;
127 connect(mMediaPlayer, &QMediaPlayer::durationChanged, [
this](qint64 duration)
129 qDebug() <<
"MediaPlayer durationChanged :" << duration;
130 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)