Add an icon (inkscape SVG)
[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 }
16 sPlayer::~sPlayer()
17 {
18     manager->~QNetworkAccessManager();
19     //reply->~QIODevice();
20     buffer->~QBuffer();
21     media->~MediaNode();
22 }
23 void sPlayer::abortDownload()
24 {
25     //pd->hide();
26     //reply->abort();
27 }
28 void sPlayer::play(QString StreamKey, QUrl server, QMaemo5Rotator::Orientation orientation)
29 {
30     this->play(StreamKey, server);
31 #ifdef Q_WS_MAEMO_5
32     if(orientation == QMaemo5Rotator::PortraitOrientation)
33         pd->rot->setCurrentOrientation(orientation);
34 #endif
35     //isPortrait = false; //just make the compilier happy on non-maemo
36 }
37
38 void sPlayer::play(QString StreamKey,QUrl server)
39 {
40     if(playing)
41     {
42         //reply->abort();
43     }
44     pd = new grooveProgressBar();
45     //pd->setAttribute();
46     pd->show();
47     pd->setValue(0);
48     QNetworkRequest req;
49     req.setUrl(server);
50     qDebug() << server;
51     req.setHeader(req.ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
52     reply = manager->post(req,QString("streamKey=" + StreamKey.toAscii()).toAscii());
53     buffer->open(buffer->ReadWrite | buffer->Truncate);
54     connect(reply,SIGNAL(finished()),this,SLOT(start()));
55     connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(putb(qint64,qint64)));
56     //connect(pd,SIGNAL(canceled()),this,SLOT(abortDownload()));
57     media->stop();
58     playing = false;
59     startStreamT = QTime::currentTime();
60 }
61 void sPlayer::start()
62 {
63     QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
64     if(url.toUrl().isValid())
65     {
66         QNetworkRequest req;
67         req.setUrl(url.toUrl());
68         qDebug() << url;
69         reply = manager->get(req);
70         connect(reply,SIGNAL(finished()),this,SLOT(start()));
71         connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(putb(qint64,qint64)));
72     }
73     else
74     {
75         if(!playing)
76         {
77             playing = true;
78             media->setCurrentSource(Phonon::MediaSource(buffer));
79             media->play();
80             pd->hide();
81             qDebug() << "Playing";
82         }
83     }
84     /*else
85     {
86         media->stop();
87         buffer->close();
88         buffer->open(QIODevice::ReadWrite | QIODevice::Truncate);
89         buffer->write(reply->readAll());
90         reply->close();
91         media->setCurrentSource(Phonon::MediaSource(buffer));
92         media->play();
93     }*/
94 }
95 void sPlayer::stop()
96 {
97     media->stop();
98 }
99
100 void sPlayer::putb(qint64 b, qint64 t)
101 {
102     //qDebug() << "Download: " << b << "Total: " << t;
103     if(b == 0 && t == 0)
104     {
105         QVariant url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
106         if(url.toUrl().isValid())
107         {
108             QNetworkRequest req;
109             req.setUrl(url.toUrl());
110             qDebug() << url;
111             reply = manager->get(req);
112             connect(reply,SIGNAL(finished()),this,SLOT(start()));
113             connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(putb(qint64,qint64)));
114         }
115         else
116         {
117             //buffer->close();
118             reply->close();
119         }
120     }
121     else
122     {
123         if(pd->maximum() != t)
124             pd->setMaximum(t);
125         pd->setValue(b);
126
127         buffer->buffer().append(reply->readAll());
128         //qDebug() << buffer->bytesAvailable();
129         if(playing)
130         {
131             StreamIO* stream = (StreamIO*) media->currentSource().stream();
132             stream->setStreamSize(buffer->size());
133         }
134
135         /*
136         //buffer->seek(b);
137         qint64 last = buffer->pos();
138         buffer->seek(buffer->bytesAvailable()+buffer->pos());
139         qDebug() << buffer->write(reply->readAll());
140         qDebug() << buffer->pos();
141         //buffer->putChar()
142         buffer->seek(last);
143         //buffer->data().append(reply->readAll());*/
144         //qDebug() << "Download speed (KB/S): " << b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024;
145         if ( b >= t*0.05 && !playing && b/(startStreamT.msecsTo(QTime::currentTime()) + 1)*100/1024 >= 10)
146         {
147             pd->hide();
148             playing = true;
149             //Start playback at 25% download
150             media->setCurrentSource(Phonon::MediaSource(buffer));
151             media->play();
152             qDebug() << "Playing";
153         }
154     }
155 }