17#include "soundplayer.h"
18#include <QAudioOutput>
19#include <QMediaPlayer>
23SoundPlayer::SoundPlayer()
27SoundPlayer::~SoundPlayer()
37 Q_ASSERT(clip !=
nullptr);
40 mMediaPlayer =
new QMediaPlayer(
this);
42 QFile file(clip->fileName());
45 mBuffer.
setData(file.readAll());
48#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
49 mMediaPlayer->setAudioOutput(
new QAudioOutput(
this));
56 clip->attachPlayer(
this);
59void SoundPlayer::onKeyFrameDestroy(
KeyFrame* keyFrame)
64bool SoundPlayer::isValid()
68 return (mMediaPlayer->error() == QMediaPlayer::NoError);
73void SoundPlayer::play()
81void SoundPlayer::pause()
85 mMediaPlayer->pause();
89void SoundPlayer::stop()
97int64_t SoundPlayer::duration()
101 return mMediaPlayer->duration();
106void SoundPlayer::setMediaPlayerPosition(qint64 pos)
110 mMediaPlayer->setPosition(pos);
114void SoundPlayer::makeConnections()
116#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
117 connect(mMediaPlayer, &QMediaPlayer::errorOccurred,
this, [](QMediaPlayer::Error err,
const QString&)
119 auto errorSignal =
static_cast<void (QMediaPlayer::*)(QMediaPlayer::Error)
>(&QMediaPlayer::error);
120 connect(mMediaPlayer, errorSignal,
this, [](QMediaPlayer::Error err)
123 qDebug() <<
"MediaPlayer Error: " << err;
126 connect(mMediaPlayer, &QMediaPlayer::durationChanged, [
this](qint64 duration)
128 qDebug() <<
"MediaPlayer durationChanged :" << duration;
129 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)