Commit dfd21740 authored by kempe's avatar kempe
Browse files

Added ability to pause seekable media

Also forward seekablechanged for later use
parent 746935ff
......@@ -11,6 +11,7 @@ AudioPlayer::AudioPlayer(QObject *parent) : QObject(parent)
connect(m_player, &QMediaPlayer::stateChanged, this, &AudioPlayer::stateChanged);
connect(m_player, &QMediaPlayer::mediaStatusChanged, this, &AudioPlayer::statusChanged);
connect(m_player, &QMediaPlayer::bufferStatusChanged, this, &AudioPlayer::bufferStatusChanged);
connect(m_player, &QMediaPlayer::seekableChanged, this, &AudioPlayer::seekableChanged);
}
AudioPlayer::~AudioPlayer()
......@@ -19,9 +20,9 @@ AudioPlayer::~AudioPlayer()
void AudioPlayer::loadUrl(QString url)
{
m_player->stop();
stopPlayback();
m_current_url = QUrl(url);
togglePlayback();
startPlayback();
}
void AudioPlayer::togglePlayback()
......@@ -29,18 +30,41 @@ void AudioPlayer::togglePlayback()
switch(m_player->state()) {
case QMediaPlayer::PausedState:
case QMediaPlayer::StoppedState:
m_player->setMedia(m_current_url);
m_player->play();
startPlayback();
break;
case QMediaPlayer::PlayingState:
m_player->stop();
m_player->setMedia(QMediaContent());
pauseOrStopPlayback();
break;
default:
break;
}
}
void AudioPlayer::startPlayback() {
if (m_player->mediaStatus() == QMediaPlayer::NoMedia) {
qDebug() << "Loading new media";
m_player->setMedia(m_current_url);
}
m_player->play();
}
void AudioPlayer::pauseOrStopPlayback() {
if (m_player->isSeekable()) {
qDebug() << "Pausing playback";
m_player->pause();
} else {
qDebug() << "Stoping playback";
stopPlayback();
}
}
void AudioPlayer::stopPlayback()
{
m_player->stop();
m_player->setMedia(QMediaContent());
}
void AudioPlayer::stateChanged(QMediaPlayer::State state)
{
m_state = state;
......
......@@ -12,6 +12,7 @@ class AudioPlayer : public QObject {
Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY playbackStateChanged)
Q_PROPERTY(bool isLoading READ isLoading NOTIFY playbackStatusChanged)
Q_PROPERTY(bool isBuffering READ isBuffering NOTIFY playbackStatusChanged)
Q_PROPERTY(bool isSeekable READ isSeekable NOTIFY seekableChanged)
Q_PROPERTY(double bufferProgress READ bufferProgress NOTIFY bufferProgressChanged)
......@@ -22,11 +23,13 @@ public:
bool isPlaying() const { return m_state == QMediaPlayer::PlayingState; }
bool isLoading() const { return m_status == QMediaPlayer::LoadingMedia; }
bool isBuffering() const { return m_status == QMediaPlayer::BufferingMedia; }
bool isSeekable() const { return m_player->isSeekable(); }
double bufferProgress() const { return m_buffer_progress; }
signals:
void playbackStateChanged();
void playbackStatusChanged();
void seekableChanged();
void bufferProgressChanged();
public slots:
......@@ -45,6 +48,10 @@ private:
QMediaPlayer::State m_state;
QMediaPlayer::MediaStatus m_status;
double m_buffer_progress;
void startPlayback();
void pauseOrStopPlayback();
void stopPlayback();
};
static QObject *audioPlayerProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment