Skip to content

Commit

Permalink
settings for volume and speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronson Mathews committed Jul 26, 2020
1 parent 0a07bdc commit 70c5c9f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ int main(int argc, char *argv[])


// create singletons
Settings* settings = Settings::instance();
Database* database = Database::instance();
Player* player = Player::instance();

Expand Down
29 changes: 25 additions & 4 deletions src/player.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include <QApplication>
#include <QSettings>
#include "player.h"
#include "database.h"
#include "util.h"
Expand Down Expand Up @@ -27,6 +28,12 @@ Player::Player(QMediaPlayer *parent)
// load settings
QString current_item = Settings::value("current_item", "").toString();
setCurrentItem(current_item);

int volume = Settings::value("volume", 100).toInt();
setVolume(volume);

int speed = Settings::value("speed", 100).toInt();
setSpeed(speed);
}


Expand Down Expand Up @@ -88,14 +95,15 @@ int Player::volume() const
return QMediaPlayer::volume();
}

qreal Player::speed() const
int Player::speed() const
{
return QMediaPlayer::playbackRate();
return QMediaPlayer::playbackRate() * 100;
}


void Player::exitHandler()
{
Settings::instance()->sync();
}


Expand Down Expand Up @@ -213,30 +221,43 @@ void Player::skipForward() {
setProgress(current_position);
}


void Player::skipBackward() {
qint64 current_position = mProgress - mSkip;
if (current_position < 0)
current_position = 0;
setProgress(current_position);
}


void Player::volumeUp() {
setVolume(volume() + 25);
}


void Player::volumeDown() {
setVolume(volume() - 25);
}


void Player::setVolume(int xVolume)
{
if (QMediaPlayer::volume() == xVolume)
return;

QMediaPlayer::setMuted(false);
QMediaPlayer::setVolume(xVolume);
Settings::setValue("volume", xVolume);
}

void Player::setSpeed(qreal xSpeed)

void Player::setSpeed(int xSpeed)
{
QMediaPlayer::setPlaybackRate(xSpeed);
if ((QMediaPlayer::playbackRate() * 100) == xSpeed)
return;

QMediaPlayer::setPlaybackRate(xSpeed/100.0);
Settings::setValue("speed", xSpeed);
}


Expand Down
8 changes: 4 additions & 4 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Player : public QMediaPlayer
Q_PROPERTY(QString chapterText READ chapterText NOTIFY currentIndexChanged)
Q_PROPERTY(QString chapterProgressText READ chapterProgressText NOTIFY currentIndexChanged)
Q_PROPERTY(int volume READ volume WRITE setVolume NOTIFY volumeChanged)
Q_PROPERTY(qreal speed READ speed WRITE setSpeed NOTIFY speedChanged)
Q_PROPERTY(int speed READ speed WRITE setSpeed NOTIFY speedChanged)

public:
~Player();
Expand All @@ -37,7 +37,7 @@ class Player : public QMediaPlayer
QString chapterProgressText() const;
int chapterIndex() const;
int volume() const;
qreal speed() const;
int speed() const;
void exitHandler();

// current/active item
Expand All @@ -58,14 +58,14 @@ public slots:
void volumeUp();
void volumeDown();
void setVolume(int xVolume);
void setSpeed(qreal xSpeed);
void setSpeed(int xSpeed);

signals:
void progressChanged();
void playlistChanged();
void currentIndexChanged(int xIndex);
void volumeChanged(int xVolume);
void speedChanged(qreal xSpeed);
void speedChanged(int xSpeed);


private:
Expand Down
8 changes: 4 additions & 4 deletions src/qml/NavigationPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Page {
to: 100
value: Player.volume
snapMode: Slider.SnapAlways
stepSize: 25
stepSize: 20

onPressedChanged: {
Player.volume = value;
Expand All @@ -77,11 +77,11 @@ Page {
id: speed_slider
anchors.top: speed_label.bottom
width: parent.width
from: 0.5
to: 1.5
from: 50
to: 150
value: Player.speed
snapMode: Slider.SnapAlways
stepSize: 0.1
stepSize: 10

onPressedChanged: {
Player.speed = value;
Expand Down
9 changes: 9 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ QObject *Settings::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
// C++ and QML instance they are the same instance
return Settings::instance();
}

Settings::Settings(QObject *parent)
: QSettings(QSettings::Format::IniFormat,
QSettings::UserScope,
QCoreApplication::instance()->organizationName(),
QCoreApplication::instance()->applicationName(),
parent)
{
}
8 changes: 3 additions & 5 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@ class Settings : public QSettings

public:
Settings(const Settings&) = delete; // disable copy for singleton

static Settings *instance();
static QObject *qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);

// Q_INVOKABLE - allow qml to access non-slot functions
Q_INVOKABLE inline static void setValue(const QString &xKey, const QVariant &xValue) {instance()->setValueImp(xKey, xValue);};
Q_INVOKABLE inline static QVariant value(const QString &xKey, const QVariant &xDefaultValue = QVariant()) { return instance()->valueImp(xKey, xDefaultValue); }
Q_INVOKABLE inline static bool valueBool(const QString &xKey, const QVariant &xDefaultValue = QVariant()) { return instance()->valueImp(xKey, xDefaultValue).toBool(); }
Q_INVOKABLE inline static QString fileName() { return instance()->fileNameImp();};

private:
explicit Settings(QObject *parent = 0) : QSettings(QSettings::UserScope,
QCoreApplication::instance()->organizationName(),
QCoreApplication::instance()->applicationName(),
parent) {} // private for singleton
explicit Settings(QObject *parent = 0); // private for singleton

// implementations
inline void setValueImp(const QString &xKey, const QVariant &xValue) { QSettings::setValue(xKey, xValue); }
inline QVariant valueImp(const QString &xKey, const QVariant &xDefaultValue = QVariant()) const { return QSettings::value(xKey, xDefaultValue); }
inline bool valueBoolImp(const QString &xKey, const QVariant &xDefaultValue = QVariant()) const { return QSettings::value(xKey, xDefaultValue).toBool(); }
inline QString fileNameImp() const { return QSettings::fileName(); }
};


Expand Down

0 comments on commit 70c5c9f

Please sign in to comment.