File indexing completed on 2024-05-05 04:50:42

0001 /*
0002    SPDX-FileCopyrightText: 2016 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
0003 
0004    SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 #ifndef MANAGEAUDIOPLAYER_H
0008 #define MANAGEAUDIOPLAYER_H
0009 
0010 #include "elisaLib_export.h"
0011 #include "elisautils.h"
0012 
0013 #include <QObject>
0014 #include <QPersistentModelIndex>
0015 #include <QAbstractItemModel>
0016 #include <QUrl>
0017 #include <QMediaPlayer>
0018 
0019 class QDateTime;
0020 
0021 class ELISALIB_EXPORT ManageAudioPlayer : public QObject
0022 {
0023     Q_OBJECT
0024 
0025     Q_PROPERTY(QPersistentModelIndex currentTrack
0026                READ currentTrack
0027                WRITE setCurrentTrack
0028                NOTIFY currentTrackChanged)
0029 
0030     Q_PROPERTY(QAbstractItemModel* playListModel
0031                READ playListModel
0032                WRITE setPlayListModel
0033                NOTIFY playListModelChanged)
0034 
0035     Q_PROPERTY(QUrl playerSource
0036                READ playerSource
0037                NOTIFY playerSourceChanged)
0038 
0039     Q_PROPERTY(int titleRole
0040                READ titleRole
0041                WRITE setTitleRole
0042                NOTIFY titleRoleChanged)
0043 
0044     Q_PROPERTY(int artistNameRole
0045                READ artistNameRole
0046                WRITE setArtistNameRole
0047                NOTIFY artistNameRoleChanged)
0048 
0049     Q_PROPERTY(int albumNameRole
0050                READ albumNameRole
0051                WRITE setAlbumNameRole
0052                NOTIFY albumNameRoleChanged)
0053 
0054     Q_PROPERTY(int urlRole
0055                READ urlRole
0056                WRITE setUrlRole
0057                NOTIFY urlRoleChanged)
0058 
0059     Q_PROPERTY(int isPlayingRole
0060                READ isPlayingRole
0061                WRITE setIsPlayingRole
0062                NOTIFY isPlayingRoleChanged)
0063 
0064     Q_PROPERTY(QMediaPlayer::MediaStatus playerStatus
0065                READ playerStatus
0066                WRITE setPlayerStatus
0067                NOTIFY playerStatusChanged)
0068 
0069     Q_PROPERTY(QMediaPlayer::PlaybackState playerPlaybackState
0070                READ playerPlaybackState
0071                WRITE setPlayerPlaybackState
0072                NOTIFY playerPlaybackStateChanged)
0073 
0074     Q_PROPERTY(QMediaPlayer::Error playerError
0075                READ playerError
0076                WRITE setPlayerError
0077                NOTIFY playerErrorChanged)
0078 
0079     Q_PROPERTY(qint64 audioDuration
0080                READ audioDuration
0081                WRITE setAudioDuration
0082                NOTIFY audioDurationChanged)
0083 
0084     Q_PROPERTY(bool playerIsSeekable
0085                READ playerIsSeekable
0086                WRITE setPlayerIsSeekable
0087                NOTIFY playerIsSeekableChanged)
0088 
0089     Q_PROPERTY(qint64 playerPosition
0090                READ playerPosition
0091                WRITE setPlayerPosition
0092                NOTIFY playerPositionChanged)
0093 
0094     Q_PROPERTY(qint64 playControlPosition
0095                READ playControlPosition
0096                WRITE setPlayControlPosition
0097                NOTIFY playControlPositionChanged)
0098 
0099     Q_PROPERTY(QVariantMap persistentState
0100                READ persistentState
0101                WRITE setPersistentState
0102                NOTIFY persistentStateChanged)
0103 
0104 public:
0105 
0106     explicit ManageAudioPlayer(QObject *parent = nullptr);
0107 
0108     [[nodiscard]] QPersistentModelIndex currentTrack() const;
0109 
0110     [[nodiscard]] QAbstractItemModel* playListModel() const;
0111 
0112     [[nodiscard]] int urlRole() const;
0113 
0114     [[nodiscard]] int isPlayingRole() const;
0115 
0116     [[nodiscard]] QUrl playerSource() const;
0117 
0118     [[nodiscard]] QMediaPlayer::MediaStatus playerStatus() const;
0119 
0120     [[nodiscard]] QMediaPlayer::PlaybackState playerPlaybackState() const;
0121 
0122     [[nodiscard]] QMediaPlayer::Error playerError() const;
0123 
0124     [[nodiscard]] qint64 audioDuration() const;
0125 
0126     [[nodiscard]] bool playerIsSeekable() const;
0127 
0128     [[nodiscard]] qint64 playerPosition() const;
0129 
0130     [[nodiscard]] qint64 playControlPosition() const;
0131 
0132     [[nodiscard]] QVariantMap persistentState() const;
0133 
0134     [[nodiscard]] int playListPosition() const;
0135 
0136     [[nodiscard]] int titleRole() const;
0137 
0138     [[nodiscard]] int artistNameRole() const;
0139 
0140     [[nodiscard]] int albumNameRole() const;
0141 
0142 Q_SIGNALS:
0143 
0144     void currentTrackChanged();
0145 
0146     void playListModelChanged();
0147 
0148     void playerSourceChanged(const QUrl &url);
0149 
0150     void urlRoleChanged();
0151 
0152     void isPlayingRoleChanged();
0153 
0154     void playerStatusChanged();
0155 
0156     void playerPlaybackStateChanged();
0157 
0158     void playerErrorChanged();
0159 
0160     void playerPlay();
0161 
0162     void playerPause();
0163 
0164     void playerStop();
0165 
0166     void skipNextTrack(ElisaUtils::SkipReason reason = ElisaUtils::SkipReason::Automatic);
0167 
0168     void audioDurationChanged();
0169 
0170     void playerIsSeekableChanged();
0171 
0172     void playerPositionChanged();
0173 
0174     void playControlPositionChanged();
0175 
0176     void persistentStateChanged();
0177 
0178     void seek(qint64 position);
0179 
0180     void saveUndoPositionInAudioWrapper(qint64 position);
0181 
0182     void restoreUndoPositionInAudioWrapper();
0183 
0184     void titleRoleChanged();
0185 
0186     void artistNameRoleChanged();
0187 
0188     void albumNameRoleChanged();
0189 
0190     void sourceInError(const QUrl &source, QMediaPlayer::Error playerError);
0191 
0192     void displayTrackError(const QString &fileName);
0193 
0194     void startedPlayingTrack(const QUrl &fileName, const QDateTime &time);
0195 
0196     void finishedPlayingTrack(const QUrl &fileName, const QDateTime &time);
0197 
0198     void updateData(const QPersistentModelIndex &index, const QVariant &value, int role);
0199 
0200 public Q_SLOTS:
0201 
0202     void setCurrentTrack(const QPersistentModelIndex &currentTrack);
0203 
0204     void saveForUndoClearPlaylist();
0205 
0206     void restoreForUndoClearPlaylist();
0207 
0208     void setPlayListModel(QAbstractItemModel* aPlayListModel);
0209 
0210     void setUrlRole(int value);
0211 
0212     void setIsPlayingRole(int value);
0213 
0214     void setPlayerStatus(QMediaPlayer::MediaStatus playerStatus);
0215 
0216     void setPlayerPlaybackState(QMediaPlayer::PlaybackState playerPlaybackState);
0217 
0218     void setPlayerError(QMediaPlayer::Error playerError);
0219 
0220     void ensurePause();
0221 
0222     void ensurePlay();
0223     
0224     void requestPlay();
0225 
0226     void playPause();
0227 
0228     void stop();
0229 
0230     void setAudioDuration(qint64 audioDuration);
0231 
0232     void setPlayerIsSeekable(bool playerIsSeekable);
0233 
0234     void setPlayerPosition(qint64 playerPosition);
0235 
0236     void setCurrentPlayingForRadios(const QString &title, const QString &nowPlaying);
0237 
0238     void setPlayControlPosition(int playerPosition);
0239 
0240     void setPersistentState(const QVariantMap &persistentStateValue);
0241 
0242     void playerSeek(int position);
0243 
0244     void playListFinished();
0245 
0246     void tracksDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
0247 
0248     void setTitleRole(int titleRole);
0249 
0250     void setArtistNameRole(int artistNameRole);
0251 
0252     void setAlbumNameRole(int albumNameRole);
0253 
0254 private:
0255 
0256     void notifyPlayerSourceProperty();
0257 
0258     void triggerPlay();
0259 
0260     void triggerPause();
0261 
0262     void triggerStop();
0263 
0264     void triggerSkipNextTrack(ElisaUtils::SkipReason reason = ElisaUtils::SkipReason::Automatic);
0265 
0266     void restorePreviousState();
0267 
0268     QPersistentModelIndex mCurrentTrack;
0269 
0270     QPersistentModelIndex mOldCurrentTrack;
0271 
0272     QAbstractItemModel *mPlayListModel = nullptr;
0273 
0274     int mTitleRole = Qt::DisplayRole;
0275 
0276     int mArtistNameRole = Qt::DisplayRole;
0277 
0278     int mAlbumNameRole = Qt::DisplayRole;
0279 
0280     int mUrlRole = Qt::DisplayRole;
0281 
0282     int mIsPlayingRole = Qt::DisplayRole;
0283 
0284     QVariant mOldPlayerSource;
0285 
0286     QMediaPlayer::MediaStatus mPlayerStatus = QMediaPlayer::NoMedia;
0287 
0288     QMediaPlayer::PlaybackState mPlayerPlaybackState = QMediaPlayer::StoppedState;
0289 
0290     QMediaPlayer::Error mPlayerError = QMediaPlayer::NoError;
0291 
0292     bool mPlayingState = false;
0293 
0294     bool mSkippingCurrentTrack = false;
0295 
0296     int mAudioDuration = 0;
0297 
0298     bool mPlayerIsSeekable = false;
0299 
0300     qint64 mPlayerPosition = 0;
0301 
0302     QVariantMap mPersistentState;
0303 
0304     bool mUndoPlayingState = false;
0305 
0306     qint64 mUndoPlayerPosition = 0;
0307 
0308 };
0309 
0310 #endif // MANAGEAUDIOPLAYER_H