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();
59 } else if (!_prev_history.isEmpty()) {
60 _current = _prev_history.pop();
63 _current = (count + (qrand() + qrand() + qrand()) % count) % count;
65 _current = _current + 1;
68 if (_random && _history.count() >= count && !_repeat ||
69 !_repeat && _current >= count) {
74 _track = _playlist.tracks().at(_current);
80 void Player::_set_source() {
81 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
82 emit trackChanged(_track);
86 if (_history.count() > 0) {
87 _prev_history.push(_current);
88 _current = _history.pop();
89 _track = _playlist.tracks().at(_current);
95 void Player::_stateChanged(Phonon::State newState, Phonon::State oldState) {
97 case Phonon::PlayingState:
98 if (_state == PLAYER_LOADING) {
99 _state = PLAYER_PLAYING;
100 emit stateChanged(_state);
103 case Phonon::StoppedState:
105 case Phonon::LoadingState:
107 case Phonon::PausedState:
108 if (_state == PLAYER_PLAYING) {
110 } else if (_state == PLAYER_ERROR) {
114 case Phonon::BufferingState:
116 case Phonon::ErrorState:
117 _state = PLAYER_ERROR;
118 qDebug() << _player->errorString();
123 void Player::_tick(qint64 ticks) {
124 int done = ticks/1000;
125 int all = _track.metadata().length();
126 emit tick(done, all);
128 _track.setCount(_track.count()+1);
129 emit trackDone(_track);
133 void Player::setPlaylist(Playlist playlist) {
134 _playlist = playlist;
136 _prev_history.clear();
140 void Player::seek(int s) {
141 _player->seek(s*1000);
144 void Player::play() {
145 _state = PLAYER_PLAYING;
146 emit stateChanged(_state);
147 if (_current == -1) {
149 _track = _playlist.tracks().at(0);
155 void Player::enqueue(int id) {