Skip to content

Commit

Permalink
tidy player
Browse files Browse the repository at this point in the history
  • Loading branch information
Gibbz committed Jul 26, 2020
1 parent 00d86f0 commit 17e82e7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
27 changes: 27 additions & 0 deletions src/book.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ class Book
}


int getChapterIndex(qint64 xPosition) {
// loop over chapters, reduce position by each chapter length till we are in the correct chapter
for(int i = 0; i < chapters.length(); ++i) {
if (xPosition < chapters[i].duration) {
return i;
}
xPosition -= chapters[i].duration;
}
return 0;
}


qint64 getChapterPosition(qint64 xProgress) {
int chapter_index = 0;
qint64 chapter_position = xProgress;
// loop over chapters, reduce position by each chapter length till we are in the correct chapter
for(int i = 0; i < chapters.length(); ++i) {
if (chapter_position < chapters[i].duration) {
chapter_index = i;
break;
}
chapter_position -= chapters[i].duration;
}
return chapter_position;
}


void ready() {
if (!isEmpty()) {
if (chapters.size() == 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class QJSEngine;
class Database : public QObject
{
Q_OBJECT
const static int DB_VERSION = 0;
const static int DB_VERSION = 1;

Q_PROPERTY(QString libraryPath READ libraryPath WRITE setLibraryPath NOTIFY pathChanged)
Q_PROPERTY(int size READ size)
Expand Down
45 changes: 18 additions & 27 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Player::Player(QMediaPlayer *parent)
connect(this, &QMediaPlayer::positionChanged, this, &Player::positionChanged);
connect(this, &QMediaPlayer::volumeChanged, this, &Player::volumeChanged);
connect(this, &QMediaPlayer::playbackRateChanged, this, &Player::speedChanged);
connect(this, &QMediaPlayer::seekableChanged, this, &Player::seekableChanged);
//connect(this, &QMediaPlayer::seekableChanged, this, &Player::seekableChanged); // broken on android
//connect(this, &QMediaPlayer::bufferStatusChanged, this, &Player::bufferChanged);

// exit app
connect(qApp, &QApplication::aboutToQuit, this, &Player::exitHandler);
Expand Down Expand Up @@ -52,6 +53,8 @@ QObject *Player::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)


void Player::loadBook() {
qDebug() << mCurrentBook->title << mCurrentBook->progress;
mSetPosition = -1;
mProgressScale = 10000.0/mCurrentBook->duration;
mProgress = mCurrentBook->progress;

Expand All @@ -62,7 +65,10 @@ void Player::loadBook() {
playlist()->addMedia(url);
}

setProgress(mCurrentBook->progress);
// set a default item in the playlist
setChapterIndex(0);

setProgress(mProgress);
}


Expand All @@ -81,7 +87,7 @@ QString Player::chapterText() const {

QString Player::chapterProgressText() const
{
if (playlist()->currentIndex() == -1)
if (chapterIndex() == -1)
return "";

QString index = QString::number(playlist()->currentIndex() +1);
Expand Down Expand Up @@ -147,18 +153,6 @@ void Player::setSliderValue(qint64 xPosition)
}


void Player::seekableChanged(bool seekable)
{
// when the audio is seekable
// we can unmute, and progress the track head
if (seekable && mSetPosition != -1) {
setPosition(mSetPosition);
setMuted(false);
mSetPosition = -1;
}
}


QString Player::positionText() const
{
if (mCurrentBook == nullptr)
Expand Down Expand Up @@ -190,7 +184,12 @@ void Player::positionChanged(qint64 xPosition)
{
// dont change progress if media is changing
if (mSetPosition > xPosition) {
//setProgress(mSetPosition);
if (QMediaPlayer::isSeekable()) {
qDebug() << "seekable!";
setPosition(mSetPosition);
setMuted(false);
mSetPosition = -1;
}
return;
}

Expand All @@ -205,17 +204,8 @@ void Player::setProgress(qint64 xPosition) {
// this converts from book progress to
// chapter index
// chapter time -> position
int chapter_index = 0;
qint64 chapter_position = xPosition;

// loop over chapters, reduce position by each chapter length till we are in the correct chapter
for(int i = 0; i < mCurrentBook->chapters.length(); ++i) {
if (chapter_position < mCurrentBook->chapters[i].duration) {
chapter_index = i;
break;
}
chapter_position -= mCurrentBook->chapters[i].duration;
}
int chapter_index = mCurrentBook->getChapterIndex(xPosition);
qint64 chapter_position = mCurrentBook->getChapterPosition(xPosition);

if (chapter_index == chapterIndex()) {
QMediaPlayer::setPosition(chapter_position);
Expand Down Expand Up @@ -252,6 +242,7 @@ void Player::volumeDown() {

void Player::setVolume(int xVolume)
{
QMediaPlayer::setMuted(false);
QMediaPlayer::setVolume(xVolume);
}

Expand Down
1 change: 0 additions & 1 deletion src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class Player : public QMediaPlayer
int volume() const;
qreal speed() const;
void exitHandler();
void seekableChanged(bool seekable);

// current/active item
void setCurrentItem(QString &xIndex);
Expand Down

0 comments on commit 17e82e7

Please sign in to comment.