kdemm Library API Documentation

simpleplayer.cpp

00001 /*  This file is part of the KDE project
00002     Copyright (C) 2004 Matthias Kretz <kretz@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License version 2 as published by the Free Software Foundation.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016     Boston, MA 02111-1307, USA.
00017 
00018 */
00019 
00020 #include "simpleplayer.h"
00021 #include <kurl.h>
00022 #include <kglobal.h>
00023 #include <kinstance.h>
00024 #include <kaboutdata.h>
00025 
00026 namespace KDE
00027 {
00028 namespace Multimedia
00029 {
00030 
00031 class SimplePlayer::Private
00032 {
00033     public:
00034         Channel * channel;
00035         Player * player;
00036 
00037         KURL url;
00038         Player::State state;
00039         float channelvolume;
00040         long time;
00041         QString title;
00042         QString type;
00043 };
00044 
00045 SimplePlayer::SimplePlayer( QObject * parent, const char * name )
00046     : QObject( parent, name )
00047     , d( new Private )
00048 {
00049     connect( Factory::self(), SIGNAL( deleteYourObjects() ), SLOT( deleteYourObjects() ) );
00050     connect( Factory::self(), SIGNAL( recreateObjects() ), SLOT( recreateObjects() ) );
00051     d->channel = Factory::self()->createChannel( KGlobal::instance()->aboutData()->programName() );
00052     d->player = Factory::self()->createPlayer();
00053     if ( d->player ) {
00054         d->player->setOutputChannel( d->channel );
00055         connect( d->player, SIGNAL( stateChanged( KDE::Multimedia::Player::State, KDE::Multimedia::Player::State ) ),
00056             SLOT( stateChanged( KDE::Multimedia::Player::State, KDE::Multimedia::Player::State ) ) );
00057         connect( d->player, SIGNAL( finished() ), SIGNAL( finished() ) );
00058     };
00059 }
00060 
00061 SimplePlayer::~SimplePlayer()
00062 {
00063     delete d->player;
00064     delete d->channel;
00065 }
00066 
00067 void SimplePlayer::play( const KURL & url )
00068 {
00069     if( ! d->player )
00070         return;
00071     if( isPaused() && url == d->url )
00072     {
00073         d->player->play();
00074         return;
00075     }
00076     if( ! d->player->load( url ) )
00077         return;
00078     d->url = url;
00079     if( d->player->state() == Player::Stopped )
00080         d->player->play();
00081 }
00082 
00083 void SimplePlayer::pause()
00084 {
00085     if( ! d->player )
00086         return;
00087     d->player->pause();
00088 }
00089 
00090 void SimplePlayer::stop()
00091 {
00092     if( ! d->player )
00093         return;
00094     d->player->stop();
00095 }
00096 
00097 long SimplePlayer::totalTime() const
00098 {
00099     if( ! d->player )
00100         return 0;
00101     return d->player->totalTime();
00102 }
00103 
00104 long SimplePlayer::currentTime() const
00105 {
00106     if( ! d->player )
00107         return 0;
00108     return d->player->currentTime();
00109 }
00110 
00111 void SimplePlayer::seek( long ms )
00112 {
00113     if( ! d->player )
00114         return;
00115     d->player->seek( ms );
00116 }
00117 
00118 float SimplePlayer::volume() const
00119 {
00120     if( ! d->player )
00121         return 0;
00122     return d->channel->volume();
00123 }
00124 
00125 void SimplePlayer::setVolume( float v )
00126 {
00127     if( ! d->player )
00128         return;
00129     d->channel->setVolume( v );
00130 }
00131 
00132 bool SimplePlayer::isPlaying() const
00133 {
00134     if( ! d->player )
00135         return false;
00136     return ( d->player->state() == Player::Playing );
00137 }
00138 
00139 bool SimplePlayer::isPaused() const
00140 {
00141     if( ! d->player )
00142         return false;
00143     return ( d->player->state() == Player::Paused );
00144 }
00145 
00146 void SimplePlayer::stateChanged( Player::State ns, Player::State os )
00147 {
00148     if( ! d->player )
00149         return;
00150     if( os == Player::Loading && ns == Player::Stopped )
00151         d->player->play();
00152 }
00153 
00154 void SimplePlayer::deleteYourObjects()
00155 {
00156     d->state = d->player->state();
00157     d->channelvolume = d->channel->volume();
00158     d->time = d->player->currentTime();
00159     d->title = d->channel->channelName();
00160     d->type = d->channel->channelType();
00161 
00162     if( d->player )
00163         d->player->stop();
00164 
00165     delete d->player;
00166     delete d->channel;
00167     d->player = 0;
00168     d->channel = 0;
00169 }
00170 
00171 void SimplePlayer::recreateObjects()
00172 {
00173     d->channel = Factory::self()->createChannel( d->title, d->type );
00174     d->channel->setVolume( d->channelvolume );
00175 
00176     d->player = Factory::self()->createPlayer();
00177     if( ! d->player )
00178         return;
00179 
00180     d->player->setOutputChannel( d->channel );
00181 
00182     if( d->state != Player::NoMedia )
00183         if( ! d->player->load( d->url ) )
00184             return;
00185     if( d->state == Player::Playing || d->state == Player::Paused || d->state == Player::Buffering )
00186     {
00187         d->player->play();
00188         d->player->seek( d->time );
00189         if( d->state == Player::Paused )
00190             d->player->pause();
00191     }
00192 }
00193 
00194 }} // namespaces
00195 
00196 #include "simpleplayer.moc"
00197 
00198 // vim: sw=4 ts=4 noet
KDE Logo
This file is part of the documentation for kdemm Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Sep 13 04:04:37 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003