00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "mediaplayer.h"
00023 #include "playlistwidget.h"
00024 #include "audiowidget.h"
00025 #include "loopcontrol.h"
00026 #include "audiodevice.h"
00027 #include "mediaplayerstate.h"
00028
00029
00030 #include <opie2/odebug.h>
00031
00032
00033 #include <qmessagebox.h>
00034
00035 extern AudioWidget *audioUI;
00036 extern PlayListWidget *playList;
00037 extern LoopControl *loopControl;
00038 extern MediaPlayerState *mediaPlayerState;
00039
00040
00041 MediaPlayer::MediaPlayer( QObject *parent, const char *name )
00042 : QObject( parent, name ), volumeDirection( 0 ), currentFile( NULL ) {
00043
00044
00045 connect( qApp,SIGNAL( aboutToQuit()),SLOT( cleanUp()) );
00046
00047 connect( mediaPlayerState, SIGNAL( playingToggled(bool) ), this, SLOT( setPlaying(bool) ) );
00048 connect( mediaPlayerState, SIGNAL( pausedToggled(bool) ), this, SLOT( pauseCheck(bool) ) );
00049 connect( mediaPlayerState, SIGNAL( next() ), this, SLOT( next() ) );
00050 connect( mediaPlayerState, SIGNAL( prev() ), this, SLOT( prev() ) );
00051
00052 connect( audioUI, SIGNAL( moreClicked() ), this, SLOT( startIncreasingVolume() ) );
00053 connect( audioUI, SIGNAL( lessClicked() ), this, SLOT( startDecreasingVolume() ) );
00054 connect( audioUI, SIGNAL( moreReleased() ), this, SLOT( stopChangingVolume() ) );
00055 connect( audioUI, SIGNAL( lessReleased() ), this, SLOT( stopChangingVolume() ) );
00056 }
00057
00058
00059 MediaPlayer::~MediaPlayer() {
00060
00061 }
00062
00063
00064 void MediaPlayer::pauseCheck( bool b ) {
00065
00066 if ( b && !mediaPlayerState->playing() )
00067 mediaPlayerState->setPaused( FALSE );
00068 }
00069
00070
00071 void MediaPlayer::play() {
00072 mediaPlayerState->setPlaying( FALSE );
00073 mediaPlayerState->setPlaying( TRUE );
00074 }
00075
00076
00077 void MediaPlayer::setPlaying( bool play ) {
00078
00079 if ( !play ) {
00080 mediaPlayerState->setPaused( FALSE );
00081 loopControl->stop( FALSE );
00082 return;
00083 }
00084
00085 if ( mediaPlayerState->paused() ) {
00086 mediaPlayerState->setPaused( FALSE );
00087 return;
00088 }
00089
00090 const DocLnk *playListCurrent = playList->current();
00091
00092 if ( playListCurrent != NULL ) {
00093 loopControl->stop( TRUE );
00094 currentFile = playListCurrent;
00095 }
00096 if ( currentFile == NULL ) {
00097 QMessageBox::critical( 0, tr( "No file"), tr( "Error: There is no file selected" ) );
00098 mediaPlayerState->setPlaying( FALSE );
00099 return;
00100 }
00101
00102 if ( ((currentFile->file()).left(4) != "http") && !QFile::exists( currentFile->file() ) ) {
00103 QMessageBox::critical( 0, tr( "File not found"),
00104 tr( "The following file was not found: <i>" )
00105 + currentFile->file() + "</i>" );
00106 mediaPlayerState->setPlaying( FALSE );
00107 return;
00108 }
00109
00110 if ( !mediaPlayerState->newDecoder( currentFile->file() ) ) {
00111 QMessageBox::critical( 0, tr( "No decoder found"),
00112 tr( "Sorry, no appropriate decoders found for this file: <i>" )
00113 + currentFile->file() + "</i>" );
00114 mediaPlayerState->setPlaying( FALSE );
00115 return;
00116 }
00117
00118 if ( !loopControl->init( currentFile->file() ) ) {
00119 QMessageBox::critical( 0, tr( "Error opening file"),
00120 tr( "Sorry, an error occured trying to play the file: <i>" ) + currentFile->file() + "</i>" );
00121 mediaPlayerState->setPlaying( FALSE );
00122 return;
00123 }
00124 long seconds = loopControl->totalPlaytime();
00125 QString time; time.sprintf("%li:%02i", seconds/60, (int)seconds%60 );
00126 QString tickerText;
00127 if( currentFile->file().left(4) == "http" )
00128 tickerText= tr( " File: " ) + currentFile->name();
00129 else
00130 tickerText = tr( " File: " ) + currentFile->name() + tr(", Length: ") + time;
00131
00132 QString fileInfo = mediaPlayerState->curDecoder()->fileInfo();
00133 if ( !fileInfo.isEmpty() )
00134 tickerText += ", " + fileInfo;
00135 audioUI->setTickerText( tickerText + "." );
00136
00137 loopControl->play();
00138
00139 mediaPlayerState->setView( loopControl->hasVideo() ? 'v' : 'a' );
00140 }
00141
00142
00143 void MediaPlayer::prev() {
00144 if ( playList->prev() )
00145 play();
00146 else if ( mediaPlayerState->looping() ) {
00147 if ( playList->last() )
00148 play();
00149 } else
00150 mediaPlayerState->setList();
00151 }
00152
00153
00154 void MediaPlayer::next() {
00155 if ( playList->next() )
00156 play();
00157 else if ( mediaPlayerState->looping() ) {
00158 if ( playList->first() )
00159 play();
00160 } else
00161 mediaPlayerState->setList();
00162 }
00163
00164
00165 void MediaPlayer::startDecreasingVolume() {
00166 volumeDirection = -1;
00167 startTimer( 100 );
00168 AudioDevice::decreaseVolume();
00169 }
00170
00171
00172 void MediaPlayer::startIncreasingVolume() {
00173 volumeDirection = +1;
00174 startTimer( 100 );
00175 AudioDevice::increaseVolume();
00176
00177 }
00178
00179 bool drawnOnScreenDisplay = FALSE;
00180 unsigned int onScreenDisplayVolume = 0;
00181 const int yoff = 110;
00182
00183 void MediaPlayer::stopChangingVolume() {
00184 killTimers();
00185
00186
00187 drawnOnScreenDisplay = FALSE;
00188 onScreenDisplayVolume = 0;
00189 int w = audioUI->width();
00190 int h = audioUI->height();
00191 audioUI->repaint( (w - 200) / 2, h - yoff, 200 + 9, 70, FALSE );
00192 }
00193
00194
00195 void MediaPlayer::timerEvent( QTimerEvent * ) {
00196
00197 if ( volumeDirection == +1 )
00198 AudioDevice::increaseVolume();
00199 else if ( volumeDirection == -1 )
00200 AudioDevice::decreaseVolume();
00201
00202
00203 unsigned int l, r, v; bool m;
00204 AudioDevice::getVolume( l, r, m );
00205 v = ((l + r) * 11) / (2*0xFFFF);
00206
00207 if ( drawnOnScreenDisplay && onScreenDisplayVolume == v ) {
00208
00209 return;
00210 }
00211
00212 int w = audioUI->width();
00213 int h = audioUI->height();
00214
00215 if ( drawnOnScreenDisplay ) {
00216 if ( onScreenDisplayVolume > v )
00217 audioUI->repaint( (w - 200) / 2 + v * 20 + 0, h - yoff + 40,
00218 (onScreenDisplayVolume - v) * 20 + 9, 30, FALSE );
00219 }
00220
00221 drawnOnScreenDisplay = TRUE;
00222 onScreenDisplayVolume = v;
00223
00224 QPainter p( audioUI );
00225 p.setPen( QColor( 0x10, 0xD0, 0x10 ) );
00226 p.setBrush( QColor( 0x10, 0xD0, 0x10 ) );
00227
00228 QFont f;
00229 f.setPixelSize( 20 );
00230 f.setBold( TRUE );
00231 p.setFont( f );
00232 p.drawText( (w - 200) / 2, h - yoff + 20, tr("Volume") );
00233
00234 for ( unsigned int i = 0; i < 10; i++ ) {
00235 if ( v > i )
00236 p.drawRect( (w - 200) / 2 + i * 20 + 0, h - yoff + 40, 9, 30 );
00237 else
00238 p.drawRect( (w - 200) / 2 + i * 20 + 3, h - yoff + 50, 3, 10 );
00239 }
00240 }
00241
00242 void MediaPlayer::keyReleaseEvent( QKeyEvent *e) {
00243 switch ( e->key() ) {
00245 case Key_Home:
00246 break;
00247 case Key_F9:
00248 break;
00249 case Key_F10:
00250 break;
00251 case Key_F11:
00252 break;
00253 case Key_F12:
00254
00255 break;
00256 case Key_F13:
00257 break;
00258 }
00259 }
00260
00261 void MediaPlayer::doBlank() {
00262
00263 }
00264
00265 void MediaPlayer::doUnblank() {
00266
00267 }
00268
00269 void MediaPlayer::cleanUp() {
00270
00271
00272
00273 }