fixed next/previous buttons, simplyfied volume

This commit is contained in:
Anika Raemer 2021-10-13 13:47:16 +02:00
parent e9a031e6d3
commit 2d985ca17d
3 changed files with 33 additions and 70 deletions

View file

@ -64,9 +64,9 @@ Item{
margins: container.margins margins: container.margins
topMargin: closeAppButton.visible ? 2*container.margins : container.margins topMargin: closeAppButton.visible ? 2*container.margins : container.margins
} }
value: musicModel.pAudioVolume value: musicModel.volume
onValueChanged: { onValueChanged: {
musicModel.pAudioVolume = value; musicModel.volume = value;
} }
} }
PlayerControlPannel { PlayerControlPannel {

View file

@ -6,10 +6,16 @@
MusicPlayer::MusicPlayer(QObject *parent) : QMediaPlayer(parent) 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 // 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) void MusicPlayer::init(NavigationItemModel *item)
@ -33,66 +39,54 @@ void MusicPlayer::navigateBack()
void MusicPlayer::playPause() void MusicPlayer::playPause()
{ {
mIsPlaying = !mIsPlaying; if(isPlaying())
emit isPlayingChanged();
if(mIsPlaying)
play();
else
pause(); pause();
else
play();
} }
void MusicPlayer::stopMusic() void MusicPlayer::stopMusic()
{ {
if(mIsPlaying){ if(isPlaying()){
mIsPlaying = false; reset();
emit isPlayingChanged(); stop();
reset();
emit stop();
} }
} }
void MusicPlayer::playNext() void MusicPlayer::playNext()
{ {
//emit next(); if(!hasNext()) return; // checks if playlist exists
if(!mIsPlaying){ playlist()->next();
mIsPlaying = true;
emit isPlayingChanged();
}
} }
void MusicPlayer::playPrevious() void MusicPlayer::playPrevious()
{ {
//emit previous(); if(!hasPrevious()) return;
if(!mIsPlaying){ playlist()->previous();
mIsPlaying = true;
emit isPlayingChanged();
}
} }
bool MusicPlayer::isPlaying() const bool MusicPlayer::isPlaying() const
{ {
return mIsPlaying; return state() == State::PlayingState;
} }
bool MusicPlayer::hasNext() const 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 bool MusicPlayer::hasPrevious() const
{ {
return mHasPrevious; if(!playlist()) return false;
} const auto previousIndex = playlist()->previousIndex();
/* player inits with currentIndex -1 and hence previousIndex = last index
void MusicPlayer::setAudioVolume(int newVolume) * until started. Yet, previousButton should initially be disabled.
{ */
if(newVolume != volume()){ return previousIndex >= 0 && previousIndex < playlist()->currentIndex();
qDebug() << volume() << newVolume;
setVolume(newVolume);
// signal audioVolumeChanged will be emitted automatically
}
} }
double MusicPlayer::getProgress() const double MusicPlayer::getProgress() const
@ -130,11 +124,6 @@ void MusicPlayer::onLengthChanged(int length)
void MusicPlayer::reset() void MusicPlayer::reset()
{ {
mHasNext = false;
mHasPrevious = false;
emit hasNextChanged();
emit hasPreviousChanged();
mCurrentMediaItemProgress = 0.0; mCurrentMediaItemProgress = 0.0;
mCurrentTime = 0.0; mCurrentTime = 0.0;
emit progressChanged(); emit progressChanged();
@ -168,7 +157,6 @@ void MusicPlayer::readMedia(const QString& path)
for(auto file:files){ for(auto file:files){
auto fileName = file.absoluteFilePath(); auto fileName = file.absoluteFilePath();
auto fileUrl = QUrl::fromLocalFile(fileName); auto fileUrl = QUrl::fromLocalFile(fileName);
qDebug() << fileName << fileUrl;
playList->addMedia(QMediaContent(fileUrl)); playList->addMedia(QMediaContent(fileUrl));
} }
} }

View file

@ -20,7 +20,6 @@ class MusicPlayer : public QMediaPlayer
Q_PROPERTY(QString pMediaLength READ getMediaLength NOTIFY mediaLengthChanged) Q_PROPERTY(QString pMediaLength READ getMediaLength NOTIFY mediaLengthChanged)
Q_PROPERTY(QString pTime READ getTime NOTIFY progressChanged) Q_PROPERTY(QString pTime READ getTime NOTIFY progressChanged)
Q_PROPERTY(QString pMediaTitle READ getMediaTitle NOTIFY mediaTitleChanged) Q_PROPERTY(QString pMediaTitle READ getMediaTitle NOTIFY mediaTitleChanged)
Q_PROPERTY(int pAudioVolume READ getAudioVolume WRITE setAudioVolume NOTIFY audioVolumeChanged)
signals: signals:
void navigateTo(NavigationItemModel *item); void navigateTo(NavigationItemModel *item);
@ -31,7 +30,6 @@ signals:
void progressChanged(); void progressChanged();
void mediaLengthChanged(); void mediaLengthChanged();
void mediaTitleChanged(); void mediaTitleChanged();
void audioVolumeChanged(int newVolume);
public: public:
MusicPlayer(QObject *parent = Q_NULLPTR); MusicPlayer(QObject *parent = Q_NULLPTR);
@ -50,25 +48,6 @@ public:
bool hasNext() const; bool hasNext() const;
bool hasPrevious() 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; double getProgress() const;
QString getMediaTitle() const; QString getMediaTitle() const;
@ -91,10 +70,6 @@ private:
void readMedia(const QString& path); void readMedia(const QString& path);
QString timeToString(int time); QString timeToString(int time);
bool mIsPlaying = false;
bool mHasNext = false;
bool mHasPrevious = false;
int mCurrentMediaItemLength = 0; int mCurrentMediaItemLength = 0;
int mCurrentTime = 0; int mCurrentTime = 0;
double mCurrentMediaItemProgress = 0; double mCurrentMediaItemProgress = 0;