1+ #pragma once
2+ #include " core/AudioEngine.hpp"
3+ #include " models/Playlist.hpp"
4+ #include " models/Song.hpp"
5+ #include " strategies/PlayStrategy.hpp"
6+ #include " enums/DeviceType.hpp"
7+ #include " enums/PlayStrategyType.hpp"
8+ #include " managers/DeviceManager.hpp"
9+ #include " managers/PlaylistManager.hpp"
10+ #include " managers/StrategyManager.hpp"
11+
12+ using namespace std ;
13+
14+ class MusicPlayerFacade {
15+ private:
16+ static MusicPlayerFacade* instance;
17+ AudioEngine* audioEngine;
18+ Playlist* loadedPlaylist;
19+ PlayStrategy* playStrategy;
20+
21+ MusicPlayerFacade () {
22+ loadedPlaylist = nullptr ;
23+ playStrategy = nullptr ;
24+ audioEngine = new AudioEngine ();
25+ }
26+
27+ public:
28+ static MusicPlayerFacade* getInstance () {
29+ if (!instance) {
30+ instance = new MusicPlayerFacade ();
31+ }
32+ return instance;
33+ }
34+
35+ void connectDevice (DeviceType deviceType) {
36+ DeviceManager::getInstance ()->connect (deviceType);
37+ }
38+
39+ void setPlayStrategy (PlayStrategyType strategyType) {
40+ playStrategy = StrategyManager::getInstance ()->getStrategy (strategyType);
41+ }
42+
43+ void loadPlaylist (const string& name) {
44+ loadedPlaylist = PlaylistManager::getInstance ()->getPlaylist (name);
45+ if (!playStrategy) {
46+ throw runtime_error (" Play strategy not set before loading." );
47+ }
48+ playStrategy->setPlaylist (loadedPlaylist);
49+ }
50+
51+ void playSong (Song* song) {
52+ if (!DeviceManager::getInstance ()->hasOutputDevice ()) {
53+ throw runtime_error (" No audio device connected." );
54+ }
55+ IAudioOutputDevice* device = DeviceManager::getInstance ()->getOutputDevice ();
56+ audioEngine->play (device, song);
57+ }
58+
59+ void pauseSong (Song* song) {
60+ if (audioEngine->getCurrentSongTitle () != song->getTitle ()) {
61+ throw runtime_error (" Cannot pause \" " + song->getTitle () + " \" ; not currently playing." );
62+ }
63+ audioEngine->pause ();
64+ }
65+
66+ void playAllTracks () {
67+ if (!loadedPlaylist) {
68+ throw runtime_error (" No playlist loaded." );
69+ }
70+ while (playStrategy->hasNext ()) {
71+ Song* nextSong = playStrategy->next ();
72+ IAudioOutputDevice* device = DeviceManager::getInstance ()->getOutputDevice ();
73+ audioEngine->play (device, nextSong);
74+ }
75+ cout << " Completed playlist: " << loadedPlaylist->getPlaylistName () << " \n " ;
76+ }
77+
78+ void playNextTrack () {
79+ if (!loadedPlaylist) {
80+ throw runtime_error (" No playlist loaded." );
81+ }
82+ if (playStrategy->hasNext ()) {
83+ Song* nextSong = playStrategy->next ();
84+ IAudioOutputDevice* device = DeviceManager::getInstance ()->getOutputDevice ();
85+ audioEngine->play (device, nextSong);
86+ }
87+ else {
88+ cout << " Completed playlist: " << loadedPlaylist->getPlaylistName () << " \n " ;
89+ }
90+ }
91+
92+ void playPreviousTrack () {
93+ if (!loadedPlaylist) {
94+ throw runtime_error (" No playlist loaded." );
95+ }
96+ if (playStrategy->hasPrevious ()) {
97+ Song* prevSong = playStrategy->previous ();
98+ IAudioOutputDevice* device = DeviceManager::getInstance ()->getOutputDevice ();
99+ audioEngine->play (device, prevSong);
100+ }
101+ else {
102+ cout << " Completed playlist: " << loadedPlaylist->getPlaylistName () << " \n " ;
103+ }
104+ }
105+
106+ void enqueueNext (Song* song) {
107+ playStrategy->addToNext (song);
108+ }
109+ };
110+
111+ MusicPlayerFacade* MusicPlayerFacade::instance = nullptr ;
112+
0 commit comments