1
/* This file is part of Clementine.
2
Copyright 2010, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "phononengine.h"
22
PhononEngine::PhononEngine()
23
: media_object_(new Phonon::MediaObject(this)),
24
audio_output_(new Phonon::AudioOutput(Phonon::MusicCategory, this)),
25
state_timer_(new QTimer(this)),
28
Phonon::createPath(media_object_, audio_output_);
30
connect(media_object_, SIGNAL(finished()), SLOT(PhononFinished()));
31
connect(media_object_, SIGNAL(stateChanged(Phonon::State,Phonon::State)), SLOT(PhononStateChanged(Phonon::State)));
33
state_timer_->setSingleShot(true);
34
connect(state_timer_, SIGNAL(timeout()), SLOT(StateTimeoutExpired()));
37
PhononEngine::~PhononEngine() {
42
bool PhononEngine::Init() {
46
bool PhononEngine::CanDecode(const QUrl &url) {
51
bool PhononEngine::Load(const QUrl &url, Engine::TrackChangeType change ) {
52
media_object_->setCurrentSource(Phonon::MediaSource(url));
56
bool PhononEngine::Play(uint offset) {
57
// The seek happens in PhononStateChanged - phonon doesn't seem to change
58
// currentTime() if we seek before we start playing :S
59
seek_offset_ = offset;
61
media_object_->play();
65
void PhononEngine::Stop() {
66
media_object_->stop();
69
void PhononEngine::Pause() {
70
media_object_->pause();
73
void PhononEngine::Unpause() {
74
media_object_->play();
77
Engine::State PhononEngine::state() const {
78
switch (media_object_->state()) {
79
case Phonon::LoadingState:
80
case Phonon::PlayingState:
81
case Phonon::BufferingState:
82
return Engine::Playing;
84
case Phonon::PausedState:
85
return Engine::Paused;
87
case Phonon::StoppedState:
88
case Phonon::ErrorState:
94
uint PhononEngine::position() const {
95
return media_object_->currentTime();
98
uint PhononEngine::length() const {
99
return media_object_->totalTime();
102
void PhononEngine::Seek(uint ms) {
103
media_object_->seek(ms);
106
void PhononEngine::SetVolumeSW(uint volume) {
107
audio_output_->setVolume(volume);
110
void PhononEngine::PhononFinished() {
114
void PhononEngine::PhononStateChanged(Phonon::State new_state) {
115
if (new_state == Phonon::ErrorState) {
116
emit Error(media_object_->errorString());
118
if (new_state == Phonon::PlayingState && seek_offset_ != -1) {
119
media_object_->seek(seek_offset_);
123
// Don't emit the state change straight away
124
state_timer_->start(100);
127
void PhononEngine::StateTimeoutExpired() {
128
emit StateChanged(state());