Skip to content

Commit 04d630e

Browse files
committed
fix progress bugs
1 parent b7d0caf commit 04d630e

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

src/player.cpp

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ Player::Player(QMediaPlayer *parent)
1616
connect(playlist, &QMediaPlaylist::currentIndexChanged, this, &Player::playlistIndexChanged);
1717

1818
connect(this, &QMediaPlayer::mediaStatusChanged, this, &Player::updateMediaStatus);
19-
connect(this, &QMediaPlayer::positionChanged, this, &Player::positionChanged);
19+
connect(this, &QMediaPlayer::positionChanged, this, &Player::updatePosition);
2020
connect(this, &QMediaPlayer::volumeChanged, this, &Player::volumeChanged);
2121
connect(this, &QMediaPlayer::playbackRateChanged, this, &Player::speedChanged);
2222

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

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

45-
4645
Repeat repeat_mode = static_cast<Repeat>(Settings::value("repeat_mode", Repeat::LIBRARY).toInt());
4746
setRepeatMode(repeat_mode);
4847
}
@@ -70,9 +69,10 @@ QObject *Player::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
7069
}
7170

7271

73-
74-
7572
void Player::setChapterIndex(int xIndex) {
73+
// disable progress update while changing chapter
74+
disconnect(this, &QMediaPlayer::positionChanged, this, &Player::updatePosition);
75+
QMediaPlayer::setMuted(true);
7676
playlist()->setCurrentIndex(xIndex);
7777
}
7878

@@ -223,20 +223,8 @@ QString Player::titleText() const
223223
}
224224

225225

226-
void Player::positionChanged(qint64 xPosition)
226+
void Player::updatePosition(qint64 xPosition)
227227
{
228-
if (mSetPosition != -1) {
229-
// dont change progress if media is changing
230-
if (!QMediaPlayer::isSeekable())
231-
return;
232-
233-
QMediaPlayer::setPosition(mSetPosition);
234-
setMuted(false);
235-
qDebug() << "unmute";
236-
mSetPosition = -1;
237-
mChangingBook = false;
238-
}
239-
240228
mProgress = mCurrentBook->getStartProgressChapter(chapterIndex()) + xPosition;
241229
mCurrentBook->progress = mProgress;
242230
emit progressChanged();
@@ -256,16 +244,15 @@ void Player::setProgress(qint64 xPosition) {
256244
}
257245

258246
// set chapter and position
259-
QMediaPlayer::setMuted(true);
260247
mSetPosition = chapter_position;
261-
qDebug() << "muted" << mSetPosition;
262248
setChapterIndex(chapter_index);
263249
}
264250

265251

266252
void Player::skipForward() {
267253
qint64 current_position = mProgress + mSkip;
268-
//if (current_position >= mPlayListTime) // TODO: check skip end of book
254+
if (current_position >= mCurrentBook->duration)
255+
current_position = mCurrentBook->duration - 1;
269256
setProgress(current_position);
270257
}
271258

@@ -344,9 +331,24 @@ void Player::updateMediaStatus(QMediaPlayer::MediaStatus xStatus)
344331
}
345332
}
346333

334+
347335
void Player::updateSeekable(bool xSeekable)
348336
{
349-
qDebug() << "seekable" << xSeekable;
337+
if (xSeekable) {
338+
// completed loading chapter
339+
connect(this, &QMediaPlayer::positionChanged, this, &Player::updatePosition);
340+
QMediaPlayer::setPosition(mSetPosition);
341+
setMuted(false);
342+
mSetPosition = -1;
343+
mChangingBook = false;
344+
}
345+
}
346+
347+
348+
void Player::updateBuffer(int xValue)
349+
{
350+
if (xValue == 100)
351+
updateSeekable(QMediaPlayer::isSeekable());
350352
}
351353

352354

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

381383
void Player::setCurrentItem(QString &xIndex)
382384
{
383-
qDebug() << "setItem" << xIndex;
384-
385385
if (xIndex.isEmpty())
386386
return;
387387

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

391+
disconnect(this, &QMediaPlayer::positionChanged, this, &Player::updatePosition);
391392
mChangingBook = true;
392393

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

395+
if (mCurrentBook != nullptr) {
396+
// if near end of book, reset progress
397+
if (mCurrentBook->duration - mCurrentBook->progress < 10000 ) // 10 seconds
398+
mCurrentBook->progress = 0;
399+
400+
// save book progress
401+
Database::instance()->writeBook(*mCurrentBook);
402+
}
397403

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

403-
qDebug() << mCurrentBook->title << mCurrentBook->progress;
404-
405409
mSetPosition = -1;
406410
mProgressScale = 10000.0/mCurrentBook->duration;
407411
mProgress = mCurrentBook->progress;
@@ -419,6 +423,7 @@ void Player::setCurrentItem(QString &xIndex)
419423
// set a default item in the playlist
420424
//setChapterIndex(0);
421425

426+
connect(this, &QMediaPlayer::positionChanged, this, &Player::updatePosition);
422427
setProgress(mProgress);
423428
}
424429

src/player.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Player : public QMediaPlayer
6868

6969
public slots:
7070
void setRepeatMode(Player::Repeat xMode);
71-
void positionChanged(qint64 xPosition);
71+
void updatePosition(qint64 xPosition);
7272
void playlistIndexChanged(int xIndex);
7373
void setProgress(qint64 xPosition);
7474
void setSliderValue(qint64 xPosition);
@@ -85,6 +85,7 @@ public slots:
8585
void setSleepTimerEnabled(bool xEnabled);
8686
void updateMediaStatus(QMediaPlayer::MediaStatus xStatus);
8787
void updateSeekable(bool xSeekable);
88+
void updateBuffer(int xValue);
8889

8990
signals:
9091
void progressChanged();

0 commit comments

Comments
 (0)