143 lines
4.3 KiB
C++
143 lines
4.3 KiB
C++
#ifndef MUSICPLAYER_H
|
|
#define MUSICPLAYER_H
|
|
|
|
#include <QObject>
|
|
#include <QMediaPlayer>
|
|
#include <models/NavigationItemModel.h>
|
|
|
|
/**
|
|
* @brief MusicPlayer providing interface to QML and Navigation.
|
|
*
|
|
* Adds media from directory referenced by the current navigation item to
|
|
* playlist and provides interface to QML MusicPlayer.
|
|
*/
|
|
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 getMediaDuration NOTIFY mediaLengthChanged)
|
|
Q_PROPERTY(QString pTime READ getPosition NOTIFY progressChanged)
|
|
Q_PROPERTY(QString pMediaTitle READ getMediaTitle NOTIFY metaDataChanged)
|
|
|
|
signals:
|
|
void navigateTo(NavigationItemModel *item);
|
|
void coverImageSourceChanged();
|
|
void hasPreviousChanged();
|
|
void hasNextChanged();
|
|
void isPlayingChanged();
|
|
void progressChanged();
|
|
void mediaLengthChanged();
|
|
|
|
public:
|
|
MusicPlayer(QObject *parent = Q_NULLPTR);
|
|
~MusicPlayer() =default;
|
|
|
|
/**
|
|
* @brief Init playlist with media from directory referenced by item.
|
|
* @param item Navigation item representing a leaf
|
|
* @see void readMedia(const QString& path)
|
|
*/
|
|
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();}
|
|
/**
|
|
* @brief Indicates whether the media player is currently playing a track.
|
|
* @return true if media player is playing a track.
|
|
*/
|
|
bool isPlaying() const;
|
|
/**
|
|
* @brief Indicates whether there is a next item in the playlist.
|
|
* @return True if there is a next item in the playlist
|
|
* Does not wrap.
|
|
*/
|
|
bool hasNext() const;
|
|
/**
|
|
* @brief Indicates whether there is a previous item in the playlist.
|
|
* @return True if there is a previous item in the playlist
|
|
* Does not wrap.
|
|
*/
|
|
bool hasPrevious() const;
|
|
|
|
/**
|
|
* @brief Get media progress as value in between 0 and 1
|
|
* @return Media progress
|
|
* 0 corresponds to the beginning and 1 to the end of the current track.
|
|
*/
|
|
double getProgress() const;
|
|
|
|
/**
|
|
* @brief Get current media title extracted from media meta data (id3 tags)
|
|
* @return Current media title
|
|
*/
|
|
QString getMediaTitle() const;
|
|
/**
|
|
* @brief Get duration of the current track
|
|
* @return Duration of the current track formatted as string
|
|
* @see QMediaPlayer::duration()
|
|
* @see millisecondsToString(int timeInMilliseconds) const
|
|
*/
|
|
QString getMediaDuration() const;
|
|
/**
|
|
* @brief Get current position within the track
|
|
* @return Postion within the track formatted as string
|
|
* @see QMediaPlayer::position()
|
|
* @see millisecondsToString(int timeInMilliseconds) const
|
|
*/
|
|
QString getPosition() const;
|
|
|
|
public slots:
|
|
/**
|
|
* @brief Navigate back to navgation list
|
|
*/
|
|
void navigateBack();
|
|
/**
|
|
* @brief Play or pause music depending on whether it is currently playing
|
|
*/
|
|
void playPause();
|
|
/**
|
|
* @brief Stop music and reset track to first track in playlist
|
|
*/
|
|
void stopMusic();
|
|
/**
|
|
* @brief Play next track in current playlist if available
|
|
*/
|
|
void playNext();
|
|
/**
|
|
* @brief Replay previoustrack in current playlist if available
|
|
*/
|
|
void playPrevious();
|
|
|
|
private:
|
|
/**
|
|
* @brief Resets playlist to first track if playlist exists and is non-empty
|
|
*/
|
|
void resetPlaylistToFirstTrack();
|
|
/**
|
|
* @brief Clear the current playlist
|
|
*/
|
|
void clearPlaylist();
|
|
/**
|
|
* @brief Add all media from path to playlist
|
|
* @param path Path to media
|
|
*/
|
|
void readMedia(const QString& path);
|
|
/**
|
|
* @brief Format time in milliseconds acoording to [M]m:ss
|
|
* @param time Time in ms
|
|
* @return formateted string
|
|
*/
|
|
QString millisecondsToString(int timeInMilliseconds) const;
|
|
|
|
NavigationItemModel* mCurrentItem = nullptr;
|
|
};
|
|
|
|
#endif // MUSICPLAYER_H
|