Playlist support (basic) and download progress dialogs
[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
38 sPlayer::~sPlayer()
39 {
40     manager->~QNetworkAccessManager();
41     //reply->~QIODevice();
42     media->~MediaNode();
43 }
44 void sPlayer::abortDownload()
45 {
46     //pd->hide();
47     //reply->abort();
48 }
49
50 void sPlayer::start(int p)
51 {
52     qDebug() << "got start play";
53     if(p == pl->currentplaying())
54         {
55             playing = true;
56             media->setCurrentSource(Phonon::MediaSource(pl->getBuffer(p)));
57             media->play();
58             qDebug() << "Playing";
59         }
60
61 }
62 void sPlayer::play()
63 {
64     if(pl->currentplaying() != -1)
65     {
66         //pl->setCurrentPlaying(pl->findFirstNotPlayed());
67     }
68     else
69         return;
70 }
71 void sPlayer::play(int p)
72 {
73     if(pl->currentplaying() != -1)
74         pl->freeMemory(pl->currentplaying());
75     pl->setCurrentPlaying(p);
76     //pl->beginDownload(p);
77 }
78
79 void sPlayer::stop()
80 {
81
82     media->stop();
83     if(pl->currentplaying() != -1)
84         pl->markPlayed(pl->currentplaying());
85     pl->setCurrentPlaying(-1);
86     playing = false;
87 }
88
89 void sPlayer::putb(int p, qint64 b, qint64 t)
90 {
91     //qDebug() << "Download: " << b << "Total: " << t;
92     if(p == pl->currentplaying())
93     {
94         if(pl->bReady(p))
95         {
96             StreamIO* stream = (StreamIO*) media->currentSource().stream();
97             stream->setStreamSize(pl->getBuffer(p)->size());
98         }
99     }
100 }