Removed debug info
[someplayer] / src / player / player.cpp
1 #include "player.h"
2 #include <phonon/MediaSource>
3
4 using namespace SomePlayer::Playback;
5 using namespace SomePlayer::DataObjects;
6
7 Player::Player(QObject *parent) :
8     QObject(parent)
9 {
10         _player = new Phonon::MediaObject(this);
11         _output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
12         _player->setTickInterval(1000);
13         connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
14         connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
15         Phonon::createPath(_player, _output);
16         int seed = reinterpret_cast<int> (_player);
17         srand(seed);
18         _random = false;
19         _repeat = false;
20 }
21
22 void Player::setTrackId(int id) {
23         _current = id;
24         _history.push(_current);
25         _set_source();
26         _state = PLAYER_LOADING;
27         emit stateChanged(_state);
28 }
29
30 void Player::play() {
31         _player->play();
32         _state = PLAYER_PLAYING;
33         emit stateChanged(_state);
34 }
35
36 void Player::stop() {
37         _player->stop();
38         _state = PLAYER_STOPPED;
39         emit stateChanged(_state);
40 }
41
42 void Player::pause() {
43         _player->pause();
44         _state = PLAYER_PAUSED;
45         emit stateChanged(_state);
46 }
47
48 void Player::next() {
49         _history.push(_current);
50         if (_random) {
51                 _current = rand() % _playlist.tracks().count();
52         } else {
53                 _current = (_current + 1) % _playlist.tracks().count();
54         }
55         if (_history.count()-1 == _playlist.tracks().count() && !_repeat) {
56                 _history.clear();
57                 stop();
58         } else {
59                 _set_source();
60                 play();
61         }
62 }
63
64 void Player::_set_source() {
65         Track track = _playlist.tracks().at(_current);
66         _player->setCurrentSource(Phonon::MediaSource(track.source()));
67         emit trackChanged(track);
68 }
69
70 void Player::prev() {
71         if (_history.count() > 0)
72                 _current = _history.pop();
73         _set_source();
74         play();
75 }
76
77 void Player::_stateChanged(Phonon::State newState, Phonon::State oldState) {
78         switch (newState) {
79         case Phonon::PlayingState:
80                 if (_state == PLAYER_LOADING) {
81                         _state = PLAYER_PLAYING;
82                         emit stateChanged(_state);
83                 }
84                 break;
85         case Phonon::StoppedState:
86                 break;
87         case Phonon::LoadingState:
88                 break;
89         case Phonon::PausedState:
90                 if (_state == PLAYER_PLAYING) {
91                         next();
92                 } else if (_state == PLAYER_ERROR) {
93                         play();
94                 }
95                 break;
96         case Phonon::BufferingState:
97                 break;
98         case Phonon::ErrorState:
99                 _state = PLAYER_ERROR;
100                 qDebug() << _player->errorString();
101                 break;
102         }
103 }
104
105 void Player::_tick(qint64 ticks) {
106         emit tick(ticks/1000, _playlist.tracks().at(_current).metadata().length());
107 }
108
109 void Player::setPlaylist(Playlist playlist) {
110         _playlist = playlist;
111         _history.clear();
112 }
113
114 void Player::seek(int s) {
115         _player->seek(s*1000);
116 }