From 6bfbbce05f7fad2503d48ca69d9f498cbce5e3ac Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Wed, 20 Jul 2011 01:52:19 +0200 Subject: [PATCH] * Fixed TWK-279 - Play first track of a playlist if play is clicked and nothing is playing yet. --- src/libtomahawk/audio/audioengine.cpp | 24 ++++++++++++++++++++++-- src/libtomahawk/audio/audioengine.h | 14 ++++++++++++-- src/libtomahawk/typedefs.h | 2 +- src/libtomahawk/viewmanager.cpp | 14 ++++++++++---- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/libtomahawk/audio/audioengine.cpp b/src/libtomahawk/audio/audioengine.cpp index 1178e0b9e..2e43c13de 100644 --- a/src/libtomahawk/audio/audioengine.cpp +++ b/src/libtomahawk/audio/audioengine.cpp @@ -46,18 +46,18 @@ AudioEngine::instance() AudioEngine::AudioEngine() : QObject() , m_isPlayingHttp( false ) -// , m_playlist( 0 ) -// , m_currentTrackPlaylist( 0 ) , m_queue( 0 ) , m_timeElapsed( 0 ) , m_expectStop( false ) , m_waitingOnNewTrack( false ) , m_infoSystemConnected( false ) + , m_state( Stopped ) { s_instance = this; qDebug() << "Init AudioEngine"; qRegisterMetaType< AudioErrorCode >("AudioErrorCode"); + qRegisterMetaType< AudioState >("AudioState"); m_mediaObject = new Phonon::MediaObject( this ); m_audioOutput = new Phonon::AudioOutput( Phonon::MusicCategory, this ); @@ -130,6 +130,7 @@ AudioEngine::pause() m_mediaObject->pause(); emit paused(); + Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( s_aeInfoIdentifier, Tomahawk::InfoSystem::InfoNowPaused, QVariant() ); } @@ -139,6 +140,7 @@ AudioEngine::stop() { qDebug() << Q_FUNC_INFO; + setState( Stopped ); m_mediaObject->stop(); if ( !m_playlist.isNull() ) @@ -227,6 +229,7 @@ AudioEngine::setVolume( int percentage ) emit volumeChanged( percentage ); } + void AudioEngine::mute() { @@ -416,6 +419,7 @@ AudioEngine::loadTrack( const Tomahawk::result_ptr& result ) return true; } + void AudioEngine::loadPreviousTrack() { @@ -513,6 +517,8 @@ AudioEngine::onStateChanged( Phonon::State newState, Phonon::State oldState ) qDebug() << "Phonon Error:" << m_mediaObject->errorString() << m_mediaObject->errorType(); return; } + if ( newState == Phonon::PlayingState ) + setState( Playing ); if ( !m_expectStop ) return; @@ -527,6 +533,10 @@ AudioEngine::onStateChanged( Phonon::State newState, Phonon::State oldState ) case Phonon::PausedState: { stopped = ( duration - 1000 < m_mediaObject->currentTime() ); + + if ( !stopped ) + setState( Paused ); + break; } case Phonon::StoppedState: @@ -614,3 +624,13 @@ AudioEngine::isLocalResult( const QString& url ) const { return url.startsWith( "file://" ); } + + +void +AudioEngine::setState( AudioState state ) +{ + AudioState oldState = m_state; + m_state = state; + + emit stateChanged( state, oldState ); +} diff --git a/src/libtomahawk/audio/audioengine.h b/src/libtomahawk/audio/audioengine.h index e57685ff5..bdb361caf 100644 --- a/src/libtomahawk/audio/audioengine.h +++ b/src/libtomahawk/audio/audioengine.h @@ -45,6 +45,7 @@ Q_OBJECT public: enum AudioErrorCode { StreamReadError, AudioDeviceError, DecodeError }; + enum AudioState { Stopped, Playing, Paused }; static AudioEngine* instance(); @@ -52,8 +53,11 @@ public: ~AudioEngine(); unsigned int volume() const { return m_audioOutput->volume() * 100.0; } // in percent - bool isPlaying() const { return m_mediaObject->state() == Phonon::PlayingState; } - bool isPaused() const { return m_mediaObject->state() == Phonon::PausedState; } + + AudioState state() const { return m_state; } + bool isPlaying() const { return m_state == Playing; } + bool isPaused() const { return m_state == Paused; } + bool isStopped() const { return m_state == Stopped; } /* Returns the PlaylistInterface of the currently playing track. Note: This might be different to the current playlist! */ Tomahawk::PlaylistInterface* currentTrackPlaylist() const { return m_currentTrackPlaylist.data(); } @@ -98,6 +102,7 @@ signals: void paused(); void resumed(); + void stateChanged( AudioState newState, AudioState oldState ); void volumeChanged( int volume /* in percent */ ); void timerMilliSeconds( qint64 msElapsed ); @@ -120,8 +125,11 @@ private slots: void setCurrentTrack( const Tomahawk::result_ptr& result ); private: + void setState( AudioState state ); + bool isHttpResult( const QString& ) const; bool isLocalResult( const QString& ) const; + void sendWaitingNotification() const; void sendNowPlayingNotification(); @@ -142,6 +150,8 @@ private: bool m_waitingOnNewTrack; bool m_infoSystemConnected; + AudioState m_state; + static AudioEngine* s_instance; }; diff --git a/src/libtomahawk/typedefs.h b/src/libtomahawk/typedefs.h index 0d6292729..f8529de91 100644 --- a/src/libtomahawk/typedefs.h +++ b/src/libtomahawk/typedefs.h @@ -55,7 +55,6 @@ namespace Tomahawk typedef QString QID; //query id typedef QString RID; //result id - enum GeneratorMode { OnDemand = 0, Static @@ -64,6 +63,7 @@ namespace Tomahawk }; // ns typedef int AudioErrorCode; +typedef int AudioState; // creates 36char ascii guid without {} around it inline static QString uuid() diff --git a/src/libtomahawk/viewmanager.cpp b/src/libtomahawk/viewmanager.cpp index d558167c0..a199ca373 100644 --- a/src/libtomahawk/viewmanager.cpp +++ b/src/libtomahawk/viewmanager.cpp @@ -410,6 +410,7 @@ ViewManager::showSuperCollection() return shown; } + void ViewManager::playlistInterfaceChanged( Tomahawk::PlaylistInterface* interface ) { @@ -570,7 +571,6 @@ ViewManager::setPage( ViewPage* page, bool trackHistory ) // save the old playlist shuffle state in config before we change playlists saveCurrentPlaylistSettings(); - unlinkPlaylist(); if ( !m_pageHistory.contains( page ) ) @@ -592,11 +592,11 @@ ViewManager::setPage( ViewPage* page, bool trackHistory ) qDebug() << "View page shown:" << page->title(); emit viewPageActivated( page ); - if( page->isTemporaryPage() ) + if ( page->isTemporaryPage() ) emit tempPageActivated( page ); - if ( !AudioEngine::instance()->playlist() ) - AudioEngine::instance()->setPlaylist( currentPlaylistInterface() ); + if ( AudioEngine::instance()->state() == AudioEngine::Stopped ) + AudioEngine::instance()->setPlaylist( page->playlistInterface() ); // UGH! if ( QObject* obj = dynamic_cast< QObject* >( currentPage() ) ) @@ -649,6 +649,7 @@ ViewManager::unlinkPlaylist() } } + void ViewManager::saveCurrentPlaylistSettings() { @@ -725,6 +726,7 @@ ViewManager::updateView() loadCurrentPlaylistSettings(); } + void ViewManager::loadCurrentPlaylistSettings() { @@ -741,6 +743,7 @@ ViewManager::loadCurrentPlaylistSettings() } } + void ViewManager::onWidgetDestroyed( QWidget* widget ) { @@ -815,18 +818,21 @@ ViewManager::createDynamicPlaylist( const Tomahawk::source_ptr& src, p->reportCreated( p ); } + ViewPage* ViewManager::pageForCollection( const collection_ptr& col ) const { return m_collectionViews.value( col, 0 ); } + ViewPage* ViewManager::pageForDynPlaylist(const dynplaylist_ptr& pl) const { return m_dynamicWidgets.value( pl, 0 ); } + ViewPage* ViewManager::pageForPlaylist(const playlist_ptr& pl) const {