#ifndef MUSICPLAYER_H #define MUSICPLAYER_H #include #include #include /** * @brief MusicPlayer providing interface to QML and Navigation */ class MusicPlayer : public QMediaPlayer { Q_OBJECT 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) Q_PROPERTY(double pProgress READ getProgress NOTIFY progressChanged) 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); void coverImageSourceChanged(); void hasPreviousChanged(); void hasNextChanged(); void isPlayingChanged(); void progressChanged(); void mediaLengthChanged(); void mediaTitleChanged(); void audioVolumeChanged(int newVolume); public: MusicPlayer(QObject *parent = Q_NULLPTR); ~MusicPlayer() =default; void init(NavigationItemModel* item); /** * @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; /** * @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; QString getMediaLength(); QString getTime(); public slots: void navigateBack(); void playPause(); void stopMusic(); void playNext(); void playPrevious(); void onTimeChanged(int time); void onLengthChanged(int length); private: void reset(); void clearMediaList(); 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; QString mMediaTitle = QString(""); NavigationItemModel* mCurrentItem = nullptr; }; #endif // MUSICPLAYER_H