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
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;

View file

@ -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{

View file

@ -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
}
}

View file

@ -5,11 +5,14 @@
#include <QMediaPlayer>
#include <models/NavigationItemModel.h>
/**
* @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;
};

View file

@ -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

View file

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

View file

@ -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.