diff --git a/LenaPi/MusicPlayer.qml b/LenaPi/MusicPlayer.qml index 78f0db4..d37e4c6 100644 --- a/LenaPi/MusicPlayer.qml +++ b/LenaPi/MusicPlayer.qml @@ -64,9 +64,9 @@ Item{ margins: container.margins topMargin: closeAppButton.visible ? 2*container.margins : container.margins } - value: musicModel.pAudioVolume + value: musicModel.volume onValueChanged: { - musicModel.pAudioVolume = value; + musicModel.volume = value; } } PlayerControlPannel { diff --git a/LenaPi/controllers/MusicPlayer.cpp b/LenaPi/controllers/MusicPlayer.cpp index 032dd13..350a03f 100644 --- a/LenaPi/controllers/MusicPlayer.cpp +++ b/LenaPi/controllers/MusicPlayer.cpp @@ -6,10 +6,16 @@ MusicPlayer::MusicPlayer(QObject *parent) : QMediaPlayer(parent) { - // 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); + /// @todo remove magic number + setVolume(50); + // relay signal to qml + connect(this, &QMediaPlayer::stateChanged, this, &MusicPlayer::isPlayingChanged); + connect(this, &QMediaPlayer::currentMediaChanged, this, [this](const QMediaContent&){ + // notify qml to refresh enable state of next and previous button + emit hasNextChanged(); + emit hasPreviousChanged(); + }); } void MusicPlayer::init(NavigationItemModel *item) @@ -33,66 +39,54 @@ void MusicPlayer::navigateBack() void MusicPlayer::playPause() { - mIsPlaying = !mIsPlaying; - emit isPlayingChanged(); - if(mIsPlaying) - play(); - else + if(isPlaying()) pause(); + else + play(); } void MusicPlayer::stopMusic() { - if(mIsPlaying){ - mIsPlaying = false; - emit isPlayingChanged(); - - reset(); - - emit stop(); + if(isPlaying()){ + reset(); + stop(); } } void MusicPlayer::playNext() { - //emit next(); - if(!mIsPlaying){ - mIsPlaying = true; - emit isPlayingChanged(); - } + if(!hasNext()) return; // checks if playlist exists + playlist()->next(); } void MusicPlayer::playPrevious() { - //emit previous(); - if(!mIsPlaying){ - mIsPlaying = true; - emit isPlayingChanged(); - } + if(!hasPrevious()) return; + playlist()->previous(); } bool MusicPlayer::isPlaying() const { - return mIsPlaying; + return state() == State::PlayingState; } bool MusicPlayer::hasNext() const { - return mHasNext; + if(!playlist()) return false; + const auto nextIndex = playlist()->nextIndex(); + /* player yields -1 as next index while playing last track + */ + return nextIndex >=0 && nextIndex < playlist()->mediaCount(); } bool MusicPlayer::hasPrevious() const { - return mHasPrevious; -} - -void MusicPlayer::setAudioVolume(int newVolume) -{ - if(newVolume != volume()){ - qDebug() << volume() << newVolume; - setVolume(newVolume); - // signal audioVolumeChanged will be emitted automatically - } + if(!playlist()) return false; + const auto previousIndex = playlist()->previousIndex(); + /* player inits with currentIndex -1 and hence previousIndex = last index + * until started. Yet, previousButton should initially be disabled. + */ + return previousIndex >= 0 && previousIndex < playlist()->currentIndex(); } double MusicPlayer::getProgress() const @@ -130,11 +124,6 @@ void MusicPlayer::onLengthChanged(int length) void MusicPlayer::reset() { - mHasNext = false; - mHasPrevious = false; - emit hasNextChanged(); - emit hasPreviousChanged(); - mCurrentMediaItemProgress = 0.0; mCurrentTime = 0.0; emit progressChanged(); @@ -168,7 +157,6 @@ void MusicPlayer::readMedia(const QString& path) for(auto file:files){ auto fileName = file.absoluteFilePath(); auto fileUrl = QUrl::fromLocalFile(fileName); - qDebug() << fileName << fileUrl; playList->addMedia(QMediaContent(fileUrl)); } } diff --git a/LenaPi/controllers/MusicPlayer.h b/LenaPi/controllers/MusicPlayer.h index 8d36b64..499a486 100644 --- a/LenaPi/controllers/MusicPlayer.h +++ b/LenaPi/controllers/MusicPlayer.h @@ -20,7 +20,6 @@ class MusicPlayer : public QMediaPlayer Q_PROPERTY(QString pMediaLength READ getMediaLength NOTIFY mediaLengthChanged) Q_PROPERTY(QString pTime READ getTime NOTIFY progressChanged) Q_PROPERTY(QString pMediaTitle READ getMediaTitle NOTIFY mediaTitleChanged) - Q_PROPERTY(int pAudioVolume READ getAudioVolume WRITE setAudioVolume NOTIFY audioVolumeChanged) signals: void navigateTo(NavigationItemModel *item); @@ -31,7 +30,6 @@ signals: void progressChanged(); void mediaLengthChanged(); void mediaTitleChanged(); - void audioVolumeChanged(int newVolume); public: MusicPlayer(QObject *parent = Q_NULLPTR); @@ -50,25 +48,6 @@ public: bool hasNext() const; bool hasPrevious() const; - /** - * @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); - double getProgress() const; QString getMediaTitle() const; @@ -91,10 +70,6 @@ private: void readMedia(const QString& path); QString timeToString(int time); - - bool mIsPlaying = false; - bool mHasNext = false; - bool mHasPrevious = false; int mCurrentMediaItemLength = 0; int mCurrentTime = 0; double mCurrentMediaItemProgress = 0;