23fe2a066d590accc25a384897d8609fe4dc2d5f
[groove] / splayer.cpp
1 #include "splayer.h"
2 #include <QFile>
3 sPlayer::sPlayer(QObject *parent) :
4     QObject(parent)
5 {
6     manager = new QNetworkAccessManager();
7     buffer = new QBuffer();
8     audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory,this);
9     media = new Phonon::MediaObject(this);
10     Phonon::createPath(media, audioOutput);
11     playing = false;
12     //reply = new QNetworkReply();
13     internal = parent;
14     //buffer->open(QIODevice::ReadWrite);
15     connect(media,SIGNAL(finished()),this,SLOT(markComplete()));
16 }
17 void sPlayer::setPlaylist(playlist *playList)
18 {
19     this->pl = playList;
20     connect(pl,SIGNAL(bufferReady(int)),this,SLOT(start(int)));
21     connect(pl,SIGNAL(downloadProgress(int,qint64,qint64)),this,SLOT(putb(int,qint64,qint64)));
22     //connect(pl,SIGNAL(downloadComplete(int)),this,SLOT(start(int)));
23
24 }
25 void sPlayer::markComplete()
26 {
27     pl->markPlayed(pl->currentplaying());
28     if(pl->existAt(pl->currentplaying()+1))
29     {
30         pl->setCurrentPlaying(pl->currentplaying()+1);
31         if(pl->bReady(pl->currentplaying()))
32             this->start(pl->currentplaying());
33     }
34     else
35         pl->setCurrentPlaying(-1);
36 }
37 void sPlayer::pause()
38 {
39     if(media->state() == Phonon::PausedState)
40         media->play();
41     if(media->state() == Phonon::PlayingState)
42         media->pause();
43 }
44 void sPlayer::playNext()
45 {
46     if(pl->existAt(pl->currentplaying()+1))
47     {
48         media->stop();
49         this->markComplete();
50     }
51 }
52
53 sPlayer::~sPlayer()
54 {
55     manager->~QNetworkAccessManager();
56     //reply->~QIODevice();
57     media->~MediaNode();
58 }
59 void sPlayer::back()
60 {
61     media->stop();
62     if(pl->existAt(pl->currentplaying()-1))
63     {
64         pl->setCurrentPlaying(pl->currentplaying()-1);
65         if(pl->bReady(pl->currentplaying()))
66             this->start(pl->currentplaying());
67     }
68     else
69         pl->setCurrentPlaying(-1);
70 }
71
72 void sPlayer::abortDownload()
73 {
74     //pd->hide();
75     //reply->abort();
76 }
77
78 void sPlayer::start(int p)
79 {
80     qDebug() << "got start play";
81     if(p == pl->currentplaying())
82         {
83             playing = true;
84             media->setCurrentSource(Phonon::MediaSource(pl->getBuffer(p)));
85             media->play();
86             qDebug() << "Playing";
87         }
88
89 }
90 void sPlayer::play()
91 {
92     if(pl->currentplaying() != -1)
93     {
94         //pl->setCurrentPlaying(pl->findFirstNotPlayed());
95     }
96     else
97         return;
98 }
99 void sPlayer::play(int p)
100 {
101     if(pl->currentplaying() != -1)
102         pl->freeMemory(pl->currentplaying());
103     pl->setCurrentPlaying(p);
104     //pl->beginDownload(p);
105 }
106
107 void sPlayer::stop()
108 {
109
110     media->stop();
111     if(pl->currentplaying() != -1)
112         pl->markPlayed(pl->currentplaying());
113     pl->setCurrentPlaying(-1);
114     playing = false;
115 }
116
117 void sPlayer::putb(int p, qint64 b, qint64 t)
118 {
119     //qDebug() << "Download: " << b << "Total: " << t;
120     if(p == pl->currentplaying())
121     {
122         if(pl->bReady(p))
123         {
124             StreamIO* stream = (StreamIO*) media->currentSource().stream();
125             stream->setStreamSize(pl->getBuffer(p)->size());
126         }
127     }
128 }