@@ -16,12 +16,12 @@ Player::Player(QMediaPlayer *parent)
16
16
connect (playlist, &QMediaPlaylist::currentIndexChanged, this , &Player::playlistIndexChanged);
17
17
18
18
connect (this , &QMediaPlayer::mediaStatusChanged, this , &Player::updateMediaStatus);
19
- connect (this , &QMediaPlayer::positionChanged, this , &Player::positionChanged );
19
+ connect (this , &QMediaPlayer::positionChanged, this , &Player::updatePosition );
20
20
connect (this , &QMediaPlayer::volumeChanged, this , &Player::volumeChanged);
21
21
connect (this , &QMediaPlayer::playbackRateChanged, this , &Player::speedChanged);
22
22
23
23
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??
25
25
26
26
mTimer = new QTimer (this );
27
27
connect (mTimer , &QTimer::timeout, this , &Player::endSleepTimer);
@@ -42,7 +42,6 @@ Player::Player(QMediaPlayer *parent)
42
42
int sleep_time = Settings::value (" sleep_time" , 3600000 ).toInt (); // 1hr in msec
43
43
setSleepTime (sleep_time);
44
44
45
-
46
45
Repeat repeat_mode = static_cast <Repeat>(Settings::value (" repeat_mode" , Repeat::LIBRARY).toInt ());
47
46
setRepeatMode (repeat_mode);
48
47
}
@@ -70,9 +69,10 @@ QObject *Player::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
70
69
}
71
70
72
71
73
-
74
-
75
72
void Player::setChapterIndex (int xIndex) {
73
+ // disable progress update while changing chapter
74
+ disconnect (this , &QMediaPlayer::positionChanged, this , &Player::updatePosition);
75
+ QMediaPlayer::setMuted (true );
76
76
playlist ()->setCurrentIndex (xIndex);
77
77
}
78
78
@@ -223,20 +223,8 @@ QString Player::titleText() const
223
223
}
224
224
225
225
226
- void Player::positionChanged (qint64 xPosition)
226
+ void Player::updatePosition (qint64 xPosition)
227
227
{
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
-
240
228
mProgress = mCurrentBook ->getStartProgressChapter (chapterIndex ()) + xPosition;
241
229
mCurrentBook ->progress = mProgress ;
242
230
emit progressChanged ();
@@ -256,16 +244,15 @@ void Player::setProgress(qint64 xPosition) {
256
244
}
257
245
258
246
// set chapter and position
259
- QMediaPlayer::setMuted (true );
260
247
mSetPosition = chapter_position;
261
- qDebug () << " muted" << mSetPosition ;
262
248
setChapterIndex (chapter_index);
263
249
}
264
250
265
251
266
252
void Player::skipForward () {
267
253
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 ;
269
256
setProgress (current_position);
270
257
}
271
258
@@ -344,9 +331,24 @@ void Player::updateMediaStatus(QMediaPlayer::MediaStatus xStatus)
344
331
}
345
332
}
346
333
334
+
347
335
void Player::updateSeekable (bool xSeekable)
348
336
{
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 ());
350
352
}
351
353
352
354
@@ -380,28 +382,30 @@ void Player::setRepeatMode(Player::Repeat xMode)
380
382
381
383
void Player::setCurrentItem (QString &xIndex)
382
384
{
383
- qDebug () << " setItem" << xIndex;
384
-
385
385
if (xIndex.isEmpty ())
386
386
return ;
387
387
388
388
if (mCurrentBook != nullptr && mCurrentBook ->path == xIndex)
389
389
return ;
390
390
391
+ disconnect (this , &QMediaPlayer::positionChanged, this , &Player::updatePosition);
391
392
mChangingBook = true ;
392
393
393
- // save book progress
394
- if (mCurrentBook != nullptr )
395
- Database::instance ()->writeBook (*mCurrentBook );
396
394
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
+ }
397
403
398
404
// load the new book to playlist
399
405
playlist ()->clear ();
400
406
Settings::setValue (" current_item" , xIndex);
401
407
mCurrentBook = Database::instance ()->getLibraryItem (xIndex);
402
408
403
- qDebug () << mCurrentBook ->title << mCurrentBook ->progress ;
404
-
405
409
mSetPosition = -1 ;
406
410
mProgressScale = 10000.0 /mCurrentBook ->duration ;
407
411
mProgress = mCurrentBook ->progress ;
@@ -419,6 +423,7 @@ void Player::setCurrentItem(QString &xIndex)
419
423
// set a default item in the playlist
420
424
// setChapterIndex(0);
421
425
426
+ connect (this , &QMediaPlayer::positionChanged, this , &Player::updatePosition);
422
427
setProgress (mProgress );
423
428
}
424
429
0 commit comments