From e9a031e6d39a55542ae3b8b8c86d98fd7debed15 Mon Sep 17 00:00:00 2001 From: Anika Raemer Date: Wed, 13 Oct 2021 13:01:19 +0200 Subject: [PATCH] volume control --- LenaPi/MusicPlayer.qml | 5 +---- LenaPi/VolumeSlider.qml | 4 ++-- LenaPi/controllers/MusicPlayer.cpp | 29 ++++++++------------------ LenaPi/controllers/MusicPlayer.h | 30 ++++++++++++++++++++------- LenaPi/main.cpp | 3 --- LenaPi/models/NavigationItemModel.cpp | 4 ++-- LenaPi/models/NavigationItemModel.h | 4 ++-- 7 files changed, 39 insertions(+), 40 deletions(-) diff --git a/LenaPi/MusicPlayer.qml b/LenaPi/MusicPlayer.qml index a34d213..78f0db4 100644 --- a/LenaPi/MusicPlayer.qml +++ b/LenaPi/MusicPlayer.qml @@ -45,7 +45,7 @@ Item{ anchors.centerIn: parent height: parent.height-10 width: height - source: musicModel.pCurrentItem.pImageSource + source: musicModel.pCoverImageSource fillMode: Image.PreserveAspectCrop layer.enabled: true layer.effect: OpacityMask{ @@ -64,9 +64,6 @@ Item{ margins: container.margins topMargin: closeAppButton.visible ? 2*container.margins : container.margins } - from: 34 // we cannot hear anything if lower than 35% - to: 100 - stepSize: 1 value: musicModel.pAudioVolume onValueChanged: { musicModel.pAudioVolume = value; diff --git a/LenaPi/VolumeSlider.qml b/LenaPi/VolumeSlider.qml index 1ca6c9b..1667df5 100644 --- a/LenaPi/VolumeSlider.qml +++ b/LenaPi/VolumeSlider.qml @@ -18,9 +18,9 @@ ColumnLayout { Layout.fillHeight: true Layout.alignment: Qt.AlignHCenter orientation: Qt.Vertical - from: 34 // we cannot hear anything if lower than 35% + from: 0 to: 100 - stepSize: 1 + stepSize: 2 value: 50 } RoundImageButton{ diff --git a/LenaPi/controllers/MusicPlayer.cpp b/LenaPi/controllers/MusicPlayer.cpp index df43e10..032dd13 100644 --- a/LenaPi/controllers/MusicPlayer.cpp +++ b/LenaPi/controllers/MusicPlayer.cpp @@ -6,11 +6,10 @@ MusicPlayer::MusicPlayer(QObject *parent) : QMediaPlayer(parent) { - /* nothing */ -} - -MusicPlayer::~MusicPlayer() -{ + // relay base class signal as NOTIFY doesn't seem to be able to handle it directly + connect(this, &QMediaPlayer::volumeChanged, this, &MusicPlayer::audioVolumeChanged); + // init audio + setAudioVolume(50); } void MusicPlayer::init(NavigationItemModel *item) @@ -19,7 +18,7 @@ void MusicPlayer::init(NavigationItemModel *item) return; } mCurrentItem = item; - emit currentItemChanged(); + emit coverImageSourceChanged(); reset(); clearMediaList(); @@ -72,11 +71,6 @@ void MusicPlayer::playPrevious() } } -NavigationItemModel *MusicPlayer::getCurrentItem() -{ - return mCurrentItem; -} - bool MusicPlayer::isPlaying() const { return mIsPlaying; @@ -94,15 +88,10 @@ bool MusicPlayer::hasPrevious() const void MusicPlayer::setAudioVolume(int newVolume) { - if(newVolume != mAudioVolume){ - if(newVolume > 100){ - mAudioVolume = 100; - } else if(newVolume < 0){ - mAudioVolume = 0; - } else { - mAudioVolume = newVolume; - } - emit audioVolumeChanged(mAudioVolume); + if(newVolume != volume()){ + qDebug() << volume() << newVolume; + setVolume(newVolume); + // signal audioVolumeChanged will be emitted automatically } } diff --git a/LenaPi/controllers/MusicPlayer.h b/LenaPi/controllers/MusicPlayer.h index 4d59071..8d36b64 100644 --- a/LenaPi/controllers/MusicPlayer.h +++ b/LenaPi/controllers/MusicPlayer.h @@ -5,11 +5,14 @@ #include #include +/** + * @brief MusicPlayer providing interface to QML and Navigation + */ class MusicPlayer : public QMediaPlayer { Q_OBJECT - Q_PROPERTY(QObject* pCurrentItem READ getCurrentItem NOTIFY currentItemChanged) + Q_PROPERTY(QUrl pCoverImageSource READ getCoverImageSource NOTIFY coverImageSourceChanged) Q_PROPERTY(bool pHasNext READ hasNext NOTIFY hasNextChanged) Q_PROPERTY(bool pHasPrevious READ hasPrevious NOTIFY hasPreviousChanged) Q_PROPERTY(bool pIsPlaying READ isPlaying NOTIFY isPlayingChanged) @@ -21,7 +24,7 @@ class MusicPlayer : public QMediaPlayer signals: void navigateTo(NavigationItemModel *item); - void currentItemChanged(); + void coverImageSourceChanged(); void hasPreviousChanged(); void hasNextChanged(); void isPlayingChanged(); @@ -32,23 +35,37 @@ signals: public: MusicPlayer(QObject *parent = Q_NULLPTR); - ~MusicPlayer(); + ~MusicPlayer() =default; void init(NavigationItemModel* item); - NavigationItemModel *getCurrentItem(); + /** + * @brief Get path to media cover image. + * @return Path to media cover image. + */ + inline const QString& getCoverImageSource() const {return mCurrentItem->getImageSource();} bool isPlaying() const; bool hasNext() const; bool hasPrevious() const; - inline int getAudioVolume() const { return mAudioVolume; } /** - * @brief Set audio volume. Information is transferred to VlcAudio + * @brief Get audio volume. Range is in between 0 and 100 + * Relays info from QMediaPlayer::volume as Q_PROPERTY does seem to have + * trouble accessing base class functions directly. + */ + inline int getAudioVolume() const { return volume(); } + /** + * @brief Set audio volume. * @param newVolume value between 0 and 100 (audio level in percent) * Ensures that volume is inbetween 0 and 100. If this range is exceeded, * the volume is set to the lowest and highest allowed value, respectively. + * + * Relay info to QMediaPlayer::setVolume as Q_PROPERTY does seem to have + * trouble accessing base class functions directly. + * + * @todo save to config file? */ void setAudioVolume(int newVolume); @@ -81,7 +98,6 @@ private: int mCurrentMediaItemLength = 0; int mCurrentTime = 0; double mCurrentMediaItemProgress = 0; - int mAudioVolume{50}; QString mMediaTitle = QString(""); NavigationItemModel* mCurrentItem = nullptr; }; diff --git a/LenaPi/main.cpp b/LenaPi/main.cpp index e4633a4..dee97c8 100644 --- a/LenaPi/main.cpp +++ b/LenaPi/main.cpp @@ -15,9 +15,6 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); QQmlApplicationEngine engine; - /* Add command line parser to specify a custom config file - * https://doc.qt.io/qt-5/qcommandlineparser.html - */ QCommandLineParser parser; parser.setApplicationDescription("Lena's music app"); // Define a custom config file using -c or --config diff --git a/LenaPi/models/NavigationItemModel.cpp b/LenaPi/models/NavigationItemModel.cpp index d540425..c4287b3 100644 --- a/LenaPi/models/NavigationItemModel.cpp +++ b/LenaPi/models/NavigationItemModel.cpp @@ -10,12 +10,12 @@ NavigationItemModel::NavigationItemModel(QObject *parent) : QObject(parent), qRegisterMetaType("NavigationItemModel*"); } -QString NavigationItemModel::getImageSource() const +const QString& NavigationItemModel::getImageSource() const { return mImageSource; } -QString NavigationItemModel::getPath() const +const QString& NavigationItemModel::getPath() const { return mPath; } diff --git a/LenaPi/models/NavigationItemModel.h b/LenaPi/models/NavigationItemModel.h index afb17b2..0bd7133 100644 --- a/LenaPi/models/NavigationItemModel.h +++ b/LenaPi/models/NavigationItemModel.h @@ -36,13 +36,13 @@ public: * represented by this item. If no such image is found, a default image is displayed * on the delegate. */ - QString getImageSource() const; + const QString &getImageSource() const; /** * @brief Get path to folder represented by this navigation item. * @return Path to folder */ - QString getPath() const; + const QString& getPath() const; /** * @brief Set folder path and set image source displayed on delegate. * @param path Path to directory that is represented by this navigation item.