2 #include <phonon/MediaSource>
4 using namespace SomePlayer::Playback;
5 using namespace SomePlayer::DataObjects;
7 Player::Player(QObject *parent) :
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);
23 void Player::setTrackId(int id) {
25 if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
26 _history.push(_current);
28 _track = _playlist.tracks().at(_current);
30 _state = PLAYER_LOADING;
31 emit stateChanged(_state);
34 void Player::toggle() {
35 if (_state == PLAYER_PLAYING) { // pause
37 _state = PLAYER_PAUSED;
38 emit stateChanged(_state);
46 _state = PLAYER_STOPPED;
47 emit stateChanged(_state);
51 int count = _playlist.tracks().count();
53 stop(); // empty playlist
56 _history.push(_current % count);
57 if (!_queue.isEmpty()) {
58 _current = _queue.dequeue();
61 _current = (count + (qrand() + qrand() + qrand()) % count) % count;
63 _current = _current + 1;
66 if (_random && _history.count() >= count && !_repeat ||
67 !_repeat && _current >= count) {
72 _track = _playlist.tracks().at(_current);
78 void Player::_set_source() {
79 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
80 emit trackChanged(_track);
84 if (_history.count() > 0) {
85 _queue.push_front(_current);
86 _current = _history.pop();
87 _track = _playlist.tracks().at(_current);
93 void Player::_stateChanged(Phonon::State newState, Phonon::State oldState) {
95 case Phonon::PlayingState:
96 if (_state == PLAYER_LOADING) {
97 _state = PLAYER_PLAYING;
98 emit stateChanged(_state);
101 case Phonon::StoppedState:
103 case Phonon::LoadingState:
105 case Phonon::PausedState:
106 if (_state == PLAYER_PLAYING) {
108 } else if (_state == PLAYER_ERROR) {
112 case Phonon::BufferingState:
114 case Phonon::ErrorState:
115 _state = PLAYER_ERROR;
116 qDebug() << _player->errorString();
121 void Player::_tick(qint64 ticks) {
122 int done = ticks/1000;
123 int all = _track.metadata().length();
124 emit tick(done, all);
126 _track.setCount(_track.count()+1);
127 emit trackDone(_track);
131 void Player::setPlaylist(Playlist playlist) {
132 _playlist = playlist;
136 void Player::seek(int s) {
137 _player->seek(s*1000);
140 void Player::play() {
141 _state = PLAYER_PLAYING;
142 emit stateChanged(_state);
143 if (_current == -1) {
145 _track = _playlist.tracks().at(0);
151 void Player::enqueue(int id) {