Change implementation of seekable in AudioManager

We cannot simply pass on the value from the underlying QMediaPlayer,
since it is not reliable (see hacks concerning restoring startup player
position).  Instead we keep track of it ourselves and send out the
signal in the proper places.
This commit is contained in:
Bart De Vries 2021-04-13 14:31:52 +02:00
parent 21feef8ce0
commit 7c4658db2d
1 changed files with 13 additions and 2 deletions

View File

@ -32,6 +32,7 @@ private:
Entry* m_entry = nullptr; Entry* m_entry = nullptr;
bool m_readyToPlay = false; bool m_readyToPlay = false;
bool playerOpen = false; bool playerOpen = false;
bool m_isSeekable = false;
bool m_lockPositionSaving = false; // sort of lock mutex to prevent updating the player position while changing sources (which will emit lots of playerPositionChanged signals) bool m_lockPositionSaving = false; // sort of lock mutex to prevent updating the player position while changing sources (which will emit lots of playerPositionChanged signals)
friend class AudioManager; friend class AudioManager;
@ -51,7 +52,7 @@ AudioManager::AudioManager(QObject *parent) : QObject(parent), d(std::make_uniqu
connect(&d->m_player, &QMediaPlayer::durationChanged, this, &AudioManager::durationChanged); connect(&d->m_player, &QMediaPlayer::durationChanged, this, &AudioManager::durationChanged);
connect(&d->m_player, &QMediaPlayer::positionChanged, this, &AudioManager::positionChanged); connect(&d->m_player, &QMediaPlayer::positionChanged, this, &AudioManager::positionChanged);
connect(&d->m_player, &QMediaPlayer::positionChanged, this, &AudioManager::savePlayPosition); connect(&d->m_player, &QMediaPlayer::positionChanged, this, &AudioManager::savePlayPosition);
connect(&d->m_player, &QMediaPlayer::seekableChanged, this, &AudioManager::seekableChanged); // we'll send custom seekableChanged signal to work around QMediaPlayer glitches
// Check if an entry was playing when the program was shut down and restore it // Check if an entry was playing when the program was shut down and restore it
if (SettingsManager::self()->lastPlayingEntry() != QStringLiteral("none")) if (SettingsManager::self()->lastPlayingEntry() != QStringLiteral("none"))
@ -112,7 +113,7 @@ qint64 AudioManager::position() const
bool AudioManager::seekable() const bool AudioManager::seekable() const
{ {
return d->m_player.isSeekable(); return d->m_isSeekable;
} }
bool AudioManager::canPlay() const bool AudioManager::canPlay() const
@ -209,12 +210,16 @@ void AudioManager::setEntry(Entry* entry)
Q_EMIT canPauseChanged(); Q_EMIT canPauseChanged();
Q_EMIT canSkipForwardChanged(); Q_EMIT canSkipForwardChanged();
Q_EMIT canSkipBackwardChanged(); Q_EMIT canSkipBackwardChanged();
d->m_isSeekable = true;
Q_EMIT seekableChanged(true);
} else { } else {
d->m_readyToPlay = false; d->m_readyToPlay = false;
Q_EMIT canPlayChanged(); Q_EMIT canPlayChanged();
Q_EMIT canPauseChanged(); Q_EMIT canPauseChanged();
Q_EMIT canSkipForwardChanged(); Q_EMIT canSkipForwardChanged();
Q_EMIT canSkipBackwardChanged(); Q_EMIT canSkipBackwardChanged();
d->m_isSeekable = false;
Q_EMIT seekableChanged(false);
} }
} }
@ -265,12 +270,16 @@ void AudioManager::play()
qDebug() << "AudioManager::play"; qDebug() << "AudioManager::play";
d->m_player.play(); d->m_player.play();
d->m_isSeekable = true;
Q_EMIT seekableChanged(d->m_isSeekable);
} }
void AudioManager::pause() void AudioManager::pause()
{ {
qDebug() << "AudioManager::pause"; qDebug() << "AudioManager::pause";
d->m_player.play();
d->m_isSeekable = true;
d->m_player.pause(); d->m_player.pause();
} }
@ -287,6 +296,8 @@ void AudioManager::stop()
qDebug() << "AudioManager::stop"; qDebug() << "AudioManager::stop";
d->m_player.stop(); d->m_player.stop();
d->m_isSeekable = false;
Q_EMIT seekableChanged(d->m_isSeekable);
} }
void AudioManager::seek(qint64 position) void AudioManager::seek(qint64 position)