volume control

This commit is contained in:
Anika Raemer 2021-10-13 13:01:19 +02:00
parent fe7bbed7c1
commit e9a031e6d3
7 changed files with 39 additions and 40 deletions

View file

@ -45,7 +45,7 @@ Item{
anchors.centerIn: parent anchors.centerIn: parent
height: parent.height-10 height: parent.height-10
width: height width: height
source: musicModel.pCurrentItem.pImageSource source: musicModel.pCoverImageSource
fillMode: Image.PreserveAspectCrop fillMode: Image.PreserveAspectCrop
layer.enabled: true layer.enabled: true
layer.effect: OpacityMask{ layer.effect: OpacityMask{
@ -64,9 +64,6 @@ Item{
margins: container.margins margins: container.margins
topMargin: closeAppButton.visible ? 2*container.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 value: musicModel.pAudioVolume
onValueChanged: { onValueChanged: {
musicModel.pAudioVolume = value; musicModel.pAudioVolume = value;

View file

@ -18,9 +18,9 @@ ColumnLayout {
Layout.fillHeight: true Layout.fillHeight: true
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter
orientation: Qt.Vertical orientation: Qt.Vertical
from: 34 // we cannot hear anything if lower than 35% from: 0
to: 100 to: 100
stepSize: 1 stepSize: 2
value: 50 value: 50
} }
RoundImageButton{ RoundImageButton{

View file

@ -6,11 +6,10 @@
MusicPlayer::MusicPlayer(QObject *parent) : QMediaPlayer(parent) MusicPlayer::MusicPlayer(QObject *parent) : QMediaPlayer(parent)
{ {
/* nothing */ // 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
MusicPlayer::~MusicPlayer() setAudioVolume(50);
{
} }
void MusicPlayer::init(NavigationItemModel *item) void MusicPlayer::init(NavigationItemModel *item)
@ -19,7 +18,7 @@ void MusicPlayer::init(NavigationItemModel *item)
return; return;
} }
mCurrentItem = item; mCurrentItem = item;
emit currentItemChanged(); emit coverImageSourceChanged();
reset(); reset();
clearMediaList(); clearMediaList();
@ -72,11 +71,6 @@ void MusicPlayer::playPrevious()
} }
} }
NavigationItemModel *MusicPlayer::getCurrentItem()
{
return mCurrentItem;
}
bool MusicPlayer::isPlaying() const bool MusicPlayer::isPlaying() const
{ {
return mIsPlaying; return mIsPlaying;
@ -94,15 +88,10 @@ bool MusicPlayer::hasPrevious() const
void MusicPlayer::setAudioVolume(int newVolume) void MusicPlayer::setAudioVolume(int newVolume)
{ {
if(newVolume != mAudioVolume){ if(newVolume != volume()){
if(newVolume > 100){ qDebug() << volume() << newVolume;
mAudioVolume = 100; setVolume(newVolume);
} else if(newVolume < 0){ // signal audioVolumeChanged will be emitted automatically
mAudioVolume = 0;
} else {
mAudioVolume = newVolume;
}
emit audioVolumeChanged(mAudioVolume);
} }
} }

View file

@ -5,11 +5,14 @@
#include <QMediaPlayer> #include <QMediaPlayer>
#include <models/NavigationItemModel.h> #include <models/NavigationItemModel.h>
/**
* @brief MusicPlayer providing interface to QML and Navigation
*/
class MusicPlayer : public QMediaPlayer class MusicPlayer : public QMediaPlayer
{ {
Q_OBJECT 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 pHasNext READ hasNext NOTIFY hasNextChanged)
Q_PROPERTY(bool pHasPrevious READ hasPrevious NOTIFY hasPreviousChanged) Q_PROPERTY(bool pHasPrevious READ hasPrevious NOTIFY hasPreviousChanged)
Q_PROPERTY(bool pIsPlaying READ isPlaying NOTIFY isPlayingChanged) Q_PROPERTY(bool pIsPlaying READ isPlaying NOTIFY isPlayingChanged)
@ -21,7 +24,7 @@ class MusicPlayer : public QMediaPlayer
signals: signals:
void navigateTo(NavigationItemModel *item); void navigateTo(NavigationItemModel *item);
void currentItemChanged(); void coverImageSourceChanged();
void hasPreviousChanged(); void hasPreviousChanged();
void hasNextChanged(); void hasNextChanged();
void isPlayingChanged(); void isPlayingChanged();
@ -32,23 +35,37 @@ signals:
public: public:
MusicPlayer(QObject *parent = Q_NULLPTR); MusicPlayer(QObject *parent = Q_NULLPTR);
~MusicPlayer(); ~MusicPlayer() =default;
void init(NavigationItemModel* item); 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 isPlaying() const;
bool hasNext() const; bool hasNext() const;
bool hasPrevious() 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) * @param newVolume value between 0 and 100 (audio level in percent)
* Ensures that volume is inbetween 0 and 100. If this range is exceeded, * 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. * 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); void setAudioVolume(int newVolume);
@ -81,7 +98,6 @@ private:
int mCurrentMediaItemLength = 0; int mCurrentMediaItemLength = 0;
int mCurrentTime = 0; int mCurrentTime = 0;
double mCurrentMediaItemProgress = 0; double mCurrentMediaItemProgress = 0;
int mAudioVolume{50};
QString mMediaTitle = QString(""); QString mMediaTitle = QString("");
NavigationItemModel* mCurrentItem = nullptr; NavigationItemModel* mCurrentItem = nullptr;
}; };

View file

@ -15,9 +15,6 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
/* Add command line parser to specify a custom config file
* https://doc.qt.io/qt-5/qcommandlineparser.html
*/
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription("Lena's music app"); parser.setApplicationDescription("Lena's music app");
// Define a custom config file using -c or --config // Define a custom config file using -c or --config

View file

@ -10,12 +10,12 @@ NavigationItemModel::NavigationItemModel(QObject *parent) : QObject(parent),
qRegisterMetaType<NavigationItemModel*>("NavigationItemModel*"); qRegisterMetaType<NavigationItemModel*>("NavigationItemModel*");
} }
QString NavigationItemModel::getImageSource() const const QString& NavigationItemModel::getImageSource() const
{ {
return mImageSource; return mImageSource;
} }
QString NavigationItemModel::getPath() const const QString& NavigationItemModel::getPath() const
{ {
return mPath; return mPath;
} }

View file

@ -36,13 +36,13 @@ public:
* represented by this item. If no such image is found, a default image is displayed * represented by this item. If no such image is found, a default image is displayed
* on the delegate. * on the delegate.
*/ */
QString getImageSource() const; const QString &getImageSource() const;
/** /**
* @brief Get path to folder represented by this navigation item. * @brief Get path to folder represented by this navigation item.
* @return Path to folder * @return Path to folder
*/ */
QString getPath() const; const QString& getPath() const;
/** /**
* @brief Set folder path and set image source displayed on delegate. * @brief Set folder path and set image source displayed on delegate.
* @param path Path to directory that is represented by this navigation item. * @param path Path to directory that is represented by this navigation item.