wc/r18865/#8604/ contributed by Mitz Pettel <mitz@webkit.org>
[kdelibs.git] / phonon / audioplayer.cpp
blob31e3341cbf74cdf4d3c42cef7ec08714cf5f68a0
1 /* This file is part of the KDE project
2 Copyright (C) 2004-2006 Matthias Kretz <kretz@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
20 #include "audioplayer.h"
21 #include "mediaobject.h"
22 #include "audiopath.h"
23 #include "audiooutput.h"
24 #include <kurl.h>
26 namespace Phonon
29 class AudioPlayer::Private
31 public:
32 Private()
33 : player( 0 )
37 MediaObject* player;
38 AudioPath* path;
39 AudioOutput* output;
40 KUrl url;
42 void _k_stateChanged( Phonon::State, Phonon::State );
45 AudioPlayer::AudioPlayer( Phonon::Category category, QObject* parent )
46 : QObject( parent )
47 , d( new Private )
49 d->output = new AudioOutput( category, this );
50 d->path = new AudioPath( this );
51 d->path->addOutput( d->output );
52 d->player = new MediaObject( this );
53 d->player->addAudioPath( d->path );
55 connect(d->player, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
56 SLOT(_k_stateChanged(Phonon::State, Phonon::State)));
57 connect( d->player, SIGNAL( finished() ), SIGNAL( finished() ) );
60 AudioPlayer::~AudioPlayer()
62 delete d->player;
63 delete d->path;
64 delete d->output;
67 void AudioPlayer::load( const KUrl& url )
69 // new URL
70 d->player->setUrl( url );
71 d->url = url;
74 void AudioPlayer::play( const KUrl& url )
76 if( url == d->url )
78 if( !isPlaying() )
79 d->player->play();
80 return;
82 // new URL
83 d->player->setUrl( url );
85 if( ErrorState == d->player->state() )
86 return;
88 d->url = url;
90 if( StoppedState == d->player->state() )
91 d->player->play();
94 void AudioPlayer::play()
96 play( d->url );
99 void AudioPlayer::pause()
101 d->player->pause();
104 void AudioPlayer::stop()
106 d->player->stop();
109 qint64 AudioPlayer::totalTime() const
111 return d->player->totalTime();
114 qint64 AudioPlayer::currentTime() const
116 return d->player->currentTime();
119 void AudioPlayer::seek( qint64 ms )
121 d->player->seek( ms );
124 float AudioPlayer::volume() const
126 return d->output->volume();
129 void AudioPlayer::setVolume( float v )
131 d->output->setVolume( v );
134 bool AudioPlayer::isPlaying() const
136 return ( d->player->state() == PlayingState );
139 bool AudioPlayer::isPaused() const
141 return ( d->player->state() == PausedState );
144 void AudioPlayer::Private::_k_stateChanged( State ns, State os )
146 if( os == LoadingState && ns == StoppedState )
147 player->play();
150 } // namespaces
152 #include "audioplayer.moc"
154 // vim: sw=4 ts=4