Skip to content

Commit 0a07bdc

Browse files
committed
tidy player
1 parent 17e82e7 commit 0a07bdc

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

src/player.cpp

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Player::Player(QMediaPlayer *parent)
1919
connect(this, &QMediaPlayer::volumeChanged, this, &Player::volumeChanged);
2020
connect(this, &QMediaPlayer::playbackRateChanged, this, &Player::speedChanged);
2121
//connect(this, &QMediaPlayer::seekableChanged, this, &Player::seekableChanged); // broken on android
22-
//connect(this, &QMediaPlayer::bufferStatusChanged, this, &Player::bufferChanged);
22+
//connect(this, &QMediaPlayer::bufferStatusChanged, this, &Player::bufferChanged); // broken
2323

2424
// exit app
2525
connect(qApp, &QApplication::aboutToQuit, this, &Player::exitHandler);
@@ -52,24 +52,6 @@ QObject *Player::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
5252
}
5353

5454

55-
void Player::loadBook() {
56-
qDebug() << mCurrentBook->title << mCurrentBook->progress;
57-
mSetPosition = -1;
58-
mProgressScale = 10000.0/mCurrentBook->duration;
59-
mProgress = mCurrentBook->progress;
60-
61-
playlist()->clear();
62-
for(auto chapter: mCurrentBook->chapters)
63-
{
64-
QUrl url = QUrl::fromLocalFile(Database::instance()->libraryPath() + chapter.path);
65-
playlist()->addMedia(url);
66-
}
67-
68-
// set a default item in the playlist
69-
setChapterIndex(0);
70-
71-
setProgress(mProgress);
72-
}
7355

7456

7557
void Player::setChapterIndex(int xIndex) {
@@ -139,6 +121,7 @@ void Player::setPlaybackMode(QMediaPlaylist::PlaybackMode mode) {
139121

140122
qint64 Player::sliderValue() const {
141123
// multiply by scale of the slider
124+
qDebug() << "slider value";
142125
return mProgress * mProgressScale;
143126
}
144127

@@ -182,17 +165,21 @@ QString Player::titleText() const
182165

183166
void Player::positionChanged(qint64 xPosition)
184167
{
185-
// dont change progress if media is changing
168+
186169
if (mSetPosition > xPosition) {
187-
if (QMediaPlayer::isSeekable()) {
188-
qDebug() << "seekable!";
189-
setPosition(mSetPosition);
190-
setMuted(false);
191-
mSetPosition = -1;
192-
}
193-
return;
170+
// dont change progress if media is changing
171+
if (!QMediaPlayer::isSeekable())
172+
return;
173+
174+
qDebug() << "seekable!";
175+
setPosition(mSetPosition);
176+
setMuted(false);
177+
mSetPosition = -1;
194178
}
195179

180+
qDebug() << "position changed emit";
181+
182+
196183
mProgress = mCurrentBook->getStartProgressChapter(chapterIndex()) + xPosition;
197184
mCurrentBook->progress = mProgress;
198185
Database::instance()->writeBook(*mCurrentBook);
@@ -204,6 +191,7 @@ void Player::setProgress(qint64 xPosition) {
204191
// this converts from book progress to
205192
// chapter index
206193
// chapter time -> position
194+
qDebug() << "setProgress";
207195
int chapter_index = mCurrentBook->getChapterIndex(xPosition);
208196
qint64 chapter_position = mCurrentBook->getChapterPosition(xPosition);
209197

@@ -260,9 +248,34 @@ void Player::setCurrentItem(QString &xIndex)
260248
if (mCurrentBook != nullptr && mCurrentBook->path == xIndex)
261249
return;
262250

263-
mCurrentBook = Database::instance()->getLibraryItem(xIndex);
251+
252+
// save book progress
253+
if (mCurrentBook != nullptr)
254+
Database::instance()->writeBook(*mCurrentBook);
255+
256+
257+
// load the new book to playlist
258+
playlist()->clear();
264259
Settings::setValue("current_item", xIndex);
265-
loadBook();
260+
mCurrentBook = Database::instance()->getLibraryItem(xIndex);
261+
262+
qDebug() << mCurrentBook->title << mCurrentBook->progress;
263+
264+
mSetPosition = -1;
265+
mProgressScale = 10000.0/mCurrentBook->duration;
266+
mProgress = mCurrentBook->progress;
267+
emit progressChanged();
268+
269+
for(auto chapter: mCurrentBook->chapters)
270+
{
271+
QUrl url = QUrl::fromLocalFile(Database::instance()->libraryPath() + chapter.path);
272+
playlist()->addMedia(url);
273+
}
274+
275+
// set a default item in the playlist
276+
//setChapterIndex(0);
277+
278+
setProgress(mProgress);
266279
}
267280

268281

src/player.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public slots:
5050
void playlistIndexChanged(int xIndex);
5151
void setProgress(qint64 xPosition);
5252
void setSliderValue(qint64 xPosition);
53-
void loadBook();
5453
void setChapterIndex(int xIndex);
5554
void setPlaybackMode(QMediaPlaylist::PlaybackMode mode);
5655
void togglePlayPause();

0 commit comments

Comments
 (0)