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);
22 void Player::setTrackId(int id) {
24 if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
25 _history.push(_current);
27 _track = _playlist.tracks().at(_current);
29 _state = PLAYER_LOADING;
30 emit stateChanged(_state);
33 void Player::toggle() {
34 if (_state == PLAYER_PLAYING) { // pause
36 _state = PLAYER_PAUSED;
37 emit stateChanged(_state);
45 _state = PLAYER_STOPPED;
46 emit stateChanged(_state);
50 int count = _playlist.tracks().count();
52 stop(); // empty playlist
55 _history.push(_current % count);
56 if (!_queue.isEmpty()) {
57 _current = _queue.dequeue();
60 _current = (count + (qrand() + qrand() + qrand()) % count) % count;
62 _current = _current + 1;
65 if (_random && _history.count() >= count && !_repeat ||
66 !_repeat && _current >= count) {
71 _track = _playlist.tracks().at(_current);
77 void Player::_set_source() {
78 _player->setCurrentSource(Phonon::MediaSource(_track.source()));
79 emit trackChanged(_track);
83 if (_history.count() > 0) {
84 _queue.push_front(_current);
85 _current = _history.pop();
86 _track = _playlist.tracks().at(_current);
92 void Player::_stateChanged(Phonon::State newState, Phonon::State oldState) {
94 case Phonon::PlayingState:
95 if (_state == PLAYER_LOADING) {
96 _state = PLAYER_PLAYING;
97 emit stateChanged(_state);
100 case Phonon::StoppedState:
102 case Phonon::LoadingState:
104 case Phonon::PausedState:
105 if (_state == PLAYER_PLAYING) {
107 } else if (_state == PLAYER_ERROR) {
111 case Phonon::BufferingState:
113 case Phonon::ErrorState:
114 _state = PLAYER_ERROR;
115 qDebug() << _player->errorString();
120 void Player::_tick(qint64 ticks) {
121 int done = ticks/1000;
122 int all = _track.metadata().length();
123 emit tick(done, all);
125 _track.setCount(_track.count()+1);
126 emit trackDone(_track);
130 void Player::setPlaylist(Playlist playlist) {
131 _playlist = playlist;
135 void Player::seek(int s) {
136 _player->seek(s*1000);
139 void Player::play() {
141 _state = PLAYER_PLAYING;
142 emit stateChanged(_state);
145 void Player::enqueue(int id) {