Fix some buid issues on maemo, Also add some simple usability fixes
[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     media->setTickInterval(200);
17     connect(media,SIGNAL(tick(qint64)),this,SLOT(updatePlayPosition(qint64)));
18 }
19 void sPlayer::setPlaylist(playlist *playList)
20 {
21     this->pl = playList;
22     connect(pl,SIGNAL(bufferReady(int)),this,SLOT(start(int)));
23     connect(pl,SIGNAL(downloadProgress(int,qint64,qint64)),this,SLOT(putb(int,qint64,qint64)));
24     //connect(pl,SIGNAL(downloadComplete(int)),this,SLOT(start(int)));
25
26 }
27 void sPlayer::markComplete()
28 {
29     pl->markPlayed(pl->currentplaying());
30     if(pl->existAt(pl->currentplaying()+1))
31     {
32         pl->setCurrentPlaying(pl->currentplaying()+1);
33         if(pl->bReady(pl->currentplaying()))
34             this->start(pl->currentplaying());
35     }
36     else
37         pl->setCurrentPlaying(-1);
38 }
39 void sPlayer::updatePlayPosition(qint64 time)
40 {
41     //qDebug() << time << ":" << media->totalTime();
42 }
43
44 void sPlayer::pause()
45 {
46     if(media->state() == Phonon::PausedState)
47         media->play();
48     if(media->state() == Phonon::PlayingState)
49         media->pause();
50 }
51 void sPlayer::playNext()
52 {
53     if(pl->existAt(pl->currentplaying()+1))
54     {
55         media->stop();
56         this->markComplete();
57     }
58 }
59
60 sPlayer::~sPlayer()
61 {
62     manager->~QNetworkAccessManager();
63     //reply->~QIODevice();
64     media->~MediaNode();
65 }
66 void sPlayer::back()
67 {
68     media->stop();
69     if(pl->existAt(pl->currentplaying()-1))
70     {
71         pl->setCurrentPlaying(pl->currentplaying()-1);
72         if(pl->bReady(pl->currentplaying()))
73             this->start(pl->currentplaying());
74     }
75     else
76         pl->setCurrentPlaying(-1);
77 }
78
79 void sPlayer::abortDownload()
80 {
81     //pd->hide();
82     //reply->abort();
83 }
84
85 void sPlayer::start(int p)
86 {
87     qDebug() << "got start play";
88     if(p == pl->currentplaying())
89         {
90             playing = true;
91             media->setCurrentSource(Phonon::MediaSource(pl->getBuffer(p)));
92             media->play();
93             qDebug() << "Playing";
94         }
95
96 }
97 void sPlayer::play()
98 {
99     if(pl->currentplaying() != -1)
100     {
101         //pl->setCurrentPlaying(pl->findFirstNotPlayed());
102     }
103     else
104         return;
105 }
106 void sPlayer::play(int p)
107 {
108     if(pl->currentplaying() != -1)
109         pl->freeMemory(pl->currentplaying());
110     pl->setCurrentPlaying(p);
111     //pl->beginDownload(p);
112 }
113
114 void sPlayer::stop()
115 {
116
117     media->stop();
118     if(pl->currentplaying() != -1)
119         pl->markPlayed(pl->currentplaying());
120     pl->setCurrentPlaying(-1);
121     playing = false;
122 }
123
124 void sPlayer::putb(int p, qint64 b, qint64 t)
125 {
126     //qDebug() << "Download: " << b << "Total: " << t;
127     if(p == pl->currentplaying())
128     {
129         if(pl->bReady(p))
130         {
131             StreamIO* stream = (StreamIO*) media->currentSource().stream();
132             stream->setStreamSize(pl->getBuffer(p)->size());
133         }
134     }
135 }