Implemented playback.
[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         qDebug() << "id: " << _current << " all: " << _playlist.tracks().count();
66         Track track = _playlist.tracks().at(_current);
67         _player->setCurrentSource(Phonon::MediaSource(track.source()));
68         emit trackChanged(track);
69 }
70
71 void Player::prev() {
72         if (_history.count() > 0)
73                 _current = _history.pop();
74         _set_source();
75         play();
76 }
77
78 void Player::_stateChanged(Phonon::State newState, Phonon::State oldState) {
79         qDebug() << "state changed: " << oldState << "->" << newState;
80         switch (newState) {
81         case Phonon::PlayingState:
82                 if (_state == PLAYER_LOADING) {
83                         _state = PLAYER_PLAYING;
84                         emit stateChanged(_state);
85                 }
86                 break;
87         case Phonon::StoppedState:
88                 break;
89         case Phonon::LoadingState:
90                 break;
91         case Phonon::PausedState:
92                 if (_state == PLAYER_PLAYING) {
93                         next();
94                 } else if (_state == PLAYER_ERROR) {
95                         play();
96                 }
97                 break;
98         case Phonon::BufferingState:
99                 break;
100         case Phonon::ErrorState:
101                 _state = PLAYER_ERROR;
102                 qDebug() << _player->errorString();
103                 break;
104         }
105 }
106
107 void Player::_tick(qint64 ticks) {
108         emit tick(ticks/1000, _playlist.tracks().at(_current).metadata().length());
109 }
110
111 void Player::setPlaylist(Playlist playlist) {
112         _playlist = playlist;
113         _history.clear();
114 }
115
116 void Player::seek(int s) {
117         qDebug() << "seeking " << s;
118         _player->seek(s*1000);
119 }