add support icons
[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::abortDownload()
60 {
61     //pd->hide();
62     //reply->abort();
63 }
64
65 void sPlayer::start(int p)
66 {
67     qDebug() << "got start play";
68     if(p == pl->currentplaying())
69         {
70             playing = true;
71             media->setCurrentSource(Phonon::MediaSource(pl->getBuffer(p)));
72             media->play();
73             qDebug() << "Playing";
74         }
75
76 }
77 void sPlayer::play()
78 {
79     if(pl->currentplaying() != -1)
80     {
81         //pl->setCurrentPlaying(pl->findFirstNotPlayed());
82     }
83     else
84         return;
85 }
86 void sPlayer::play(int p)
87 {
88     if(pl->currentplaying() != -1)
89         pl->freeMemory(pl->currentplaying());
90     pl->setCurrentPlaying(p);
91     //pl->beginDownload(p);
92 }
93
94 void sPlayer::stop()
95 {
96
97     media->stop();
98     if(pl->currentplaying() != -1)
99         pl->markPlayed(pl->currentplaying());
100     pl->setCurrentPlaying(-1);
101     playing = false;
102 }
103
104 void sPlayer::putb(int p, qint64 b, qint64 t)
105 {
106     //qDebug() << "Download: " << b << "Total: " << t;
107     if(p == pl->currentplaying())
108     {
109         if(pl->bReady(p))
110         {
111             StreamIO* stream = (StreamIO*) media->currentSource().stream();
112             stream->setStreamSize(pl->getBuffer(p)->size());
113         }
114     }
115 }