Skip to content

Commit

Permalink
fix progress bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gibbz committed Jul 31, 2020
1 parent b7d0caf commit 04d630e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
63 changes: 34 additions & 29 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Player::Player(QMediaPlayer *parent)
connect(playlist, &QMediaPlaylist::currentIndexChanged, this, &Player::playlistIndexChanged);

connect(this, &QMediaPlayer::mediaStatusChanged, this, &Player::updateMediaStatus);
connect(this, &QMediaPlayer::positionChanged, this, &Player::positionChanged);
connect(this, &QMediaPlayer::positionChanged, this, &Player::updatePosition);
connect(this, &QMediaPlayer::volumeChanged, this, &Player::volumeChanged);
connect(this, &QMediaPlayer::playbackRateChanged, this, &Player::speedChanged);

connect(this, &QMediaPlayer::seekableChanged, this, &Player::updateSeekable); // broken on android
//connect(this, &QMediaPlayer::bufferStatusChanged, this, &Player::bufferChanged); // broken
connect(this, &QMediaPlayer::bufferStatusChanged, this, &Player::updateBuffer); // broken on desktop??

mTimer = new QTimer(this);
connect(mTimer, &QTimer::timeout, this, &Player::endSleepTimer);
Expand All @@ -42,7 +42,6 @@ Player::Player(QMediaPlayer *parent)
int sleep_time = Settings::value("sleep_time", 3600000).toInt(); // 1hr in msec
setSleepTime(sleep_time);


Repeat repeat_mode = static_cast<Repeat>(Settings::value("repeat_mode", Repeat::LIBRARY).toInt());
setRepeatMode(repeat_mode);
}
Expand Down Expand Up @@ -70,9 +69,10 @@ QObject *Player::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
}




void Player::setChapterIndex(int xIndex) {
// disable progress update while changing chapter
disconnect(this, &QMediaPlayer::positionChanged, this, &Player::updatePosition);
QMediaPlayer::setMuted(true);
playlist()->setCurrentIndex(xIndex);
}

Expand Down Expand Up @@ -223,20 +223,8 @@ QString Player::titleText() const
}


void Player::positionChanged(qint64 xPosition)
void Player::updatePosition(qint64 xPosition)
{
if (mSetPosition != -1) {
// dont change progress if media is changing
if (!QMediaPlayer::isSeekable())
return;

QMediaPlayer::setPosition(mSetPosition);
setMuted(false);
qDebug() << "unmute";
mSetPosition = -1;
mChangingBook = false;
}

mProgress = mCurrentBook->getStartProgressChapter(chapterIndex()) + xPosition;
mCurrentBook->progress = mProgress;
emit progressChanged();
Expand All @@ -256,16 +244,15 @@ void Player::setProgress(qint64 xPosition) {
}

// set chapter and position
QMediaPlayer::setMuted(true);
mSetPosition = chapter_position;
qDebug() << "muted" << mSetPosition;
setChapterIndex(chapter_index);
}


void Player::skipForward() {
qint64 current_position = mProgress + mSkip;
//if (current_position >= mPlayListTime) // TODO: check skip end of book
if (current_position >= mCurrentBook->duration)
current_position = mCurrentBook->duration - 1;
setProgress(current_position);
}

Expand Down Expand Up @@ -344,9 +331,24 @@ void Player::updateMediaStatus(QMediaPlayer::MediaStatus xStatus)
}
}


void Player::updateSeekable(bool xSeekable)
{
qDebug() << "seekable" << xSeekable;
if (xSeekable) {
// completed loading chapter
connect(this, &QMediaPlayer::positionChanged, this, &Player::updatePosition);
QMediaPlayer::setPosition(mSetPosition);
setMuted(false);
mSetPosition = -1;
mChangingBook = false;
}
}


void Player::updateBuffer(int xValue)
{
if (xValue == 100)
updateSeekable(QMediaPlayer::isSeekable());
}


Expand Down Expand Up @@ -380,28 +382,30 @@ void Player::setRepeatMode(Player::Repeat xMode)

void Player::setCurrentItem(QString &xIndex)
{
qDebug() << "setItem" << xIndex;

if (xIndex.isEmpty())
return;

if (mCurrentBook != nullptr && mCurrentBook->path == xIndex)
return;

disconnect(this, &QMediaPlayer::positionChanged, this, &Player::updatePosition);
mChangingBook = true;

// save book progress
if (mCurrentBook != nullptr)
Database::instance()->writeBook(*mCurrentBook);

if (mCurrentBook != nullptr) {
// if near end of book, reset progress
if (mCurrentBook->duration - mCurrentBook->progress < 10000 ) // 10 seconds
mCurrentBook->progress = 0;

// save book progress
Database::instance()->writeBook(*mCurrentBook);
}

// load the new book to playlist
playlist()->clear();
Settings::setValue("current_item", xIndex);
mCurrentBook = Database::instance()->getLibraryItem(xIndex);

qDebug() << mCurrentBook->title << mCurrentBook->progress;

mSetPosition = -1;
mProgressScale = 10000.0/mCurrentBook->duration;
mProgress = mCurrentBook->progress;
Expand All @@ -419,6 +423,7 @@ void Player::setCurrentItem(QString &xIndex)
// set a default item in the playlist
//setChapterIndex(0);

connect(this, &QMediaPlayer::positionChanged, this, &Player::updatePosition);
setProgress(mProgress);
}

Expand Down
3 changes: 2 additions & 1 deletion src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Player : public QMediaPlayer

public slots:
void setRepeatMode(Player::Repeat xMode);
void positionChanged(qint64 xPosition);
void updatePosition(qint64 xPosition);
void playlistIndexChanged(int xIndex);
void setProgress(qint64 xPosition);
void setSliderValue(qint64 xPosition);
Expand All @@ -85,6 +85,7 @@ public slots:
void setSleepTimerEnabled(bool xEnabled);
void updateMediaStatus(QMediaPlayer::MediaStatus xStatus);
void updateSeekable(bool xSeekable);
void updateBuffer(int xValue);

signals:
void progressChanged();
Expand Down

0 comments on commit 04d630e

Please sign in to comment.