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

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