photosgallery/slideshow/engine/controlsrc/shwmusiccontrol.cpp
changeset 0 4e91876724a2
child 30 a60acebbbd9d
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    The music control for the slideshow
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 //  CLASS HEADER
       
    22 #include "shwmusiccontrol.h"
       
    23 
       
    24 //  EXTERNAL INCLUDES
       
    25 #include <f32file.h>    // RFs
       
    26 #include <mpxplaybackutility.h>     // MMPXPlaybackUtility
       
    27 #include <mpxplaybackframeworkdefs.h> // TMPXPlaybackProperty, EPbRepeatOne
       
    28 #include <mpxplaybackmessage.h> // TMPXPlaybackMessage
       
    29 #include <mpxmessagegeneraldefs.h>
       
    30 
       
    31 //  INTERNAL INCLUDES
       
    32 #include "shwevent.h"
       
    33 #include "shwslideshowenginepanic.h"
       
    34 
       
    35 #include <glxlog.h>
       
    36 #include <glxtracer.h>
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // C++ Constructor. save a few bits of ROM by inlining it.
       
    40 // -----------------------------------------------------------------------------
       
    41 inline CShwMusicControl::CShwMusicControl(
       
    42             MShwMusicObserver& aMusicObsvr, 
       
    43             const TDesC& aFilePath )
       
    44         : iFilePath( aFilePath ), 
       
    45         iMusicObsvr( aMusicObsvr ),
       
    46         iMaxVolume( KErrNotFound ),
       
    47         iCurrentVolume( KErrNotFound ),
       
    48         iState( EMusicOff )
       
    49     {
       
    50     // No implementation required
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // NewL. Static construction
       
    55 // -----------------------------------------------------------------------------
       
    56 CShwMusicControl* CShwMusicControl::NewL(
       
    57     MShwMusicObserver& aMusicObsvr, const TDesC& aFilePath )
       
    58     {
       
    59     TRACER(" CShwMusicControl::NewL");
       
    60     GLX_LOG_INFO( "CShwMusicControl::NewL" );
       
    61     CShwMusicControl* self = 
       
    62         new( ELeave ) CShwMusicControl( aMusicObsvr, aFilePath );
       
    63     CleanupStack::PushL( self );
       
    64     self->ConstructL();
       
    65     CleanupStack::Pop( self );
       
    66     return self;
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // Destructor.
       
    71 // -----------------------------------------------------------------------------
       
    72 CShwMusicControl::~CShwMusicControl()
       
    73     {
       
    74     TRACER(" CShwMusicControl::~CShwMusicControl");
       
    75     GLX_LOG_INFO( "CShwMusicControl::~CShwMusicControl" );
       
    76     if( iPlaybackUtility )
       
    77         {
       
    78         //The Code Scanner Error is not corrected here in the case of
       
    79         //calling a leaving function in the Non Leaving Function
       
    80         //We need to write a seperate Function for this.
       
    81         //but will reduce not reduce any error.
       
    82         iPlaybackUtility->RemoveObserverL(*this);
       
    83         // Tell player to close the music playback
       
    84         TRAP_IGNORE( iPlaybackUtility->CommandL( EPbCmdClose ) );
       
    85 
       
    86 
       
    87 
       
    88         // Don't destroy, close releases resources.
       
    89         iPlaybackUtility->Close();
       
    90         }
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // ConstructL.
       
    95 // -----------------------------------------------------------------------------
       
    96 void CShwMusicControl::ConstructL()
       
    97     {
       
    98     TRACER("CShwMusicControl::ConstructL");
       
    99     GLX_LOG_INFO("CShwMusicControl::ConstructL");
       
   100     // need to specify the mode and observer, without these we get a crash
       
   101     iPlaybackUtility = MMPXPlaybackUtility::NewL( KPbModeNewPlayer, this );
       
   102     // music playback is sacrificed if MPX fails
       
   103     TRAPD( err, InitPlayerL() );
       
   104     if( err == KErrNone )
       
   105         {
       
   106         iState = EMusicOn;
       
   107         }
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // Initialize the player with File Path
       
   112 // ---------------------------------------------------------------------------
       
   113 void CShwMusicControl::InitPlayerL()
       
   114     {
       
   115     TRACER("CShwMusicControl::InitPlayerL");
       
   116     GLX_LOG_INFO( "CShwMusicControl::InitPlayerL" );
       
   117     RFs fs;
       
   118     User::LeaveIfError( fs.Connect() );
       
   119     CleanupClosePushL( fs );
       
   120     User::LeaveIfError( fs.ShareProtected() );
       
   121     RFile file;
       
   122     // try to open the file
       
   123     TInt error = file.Open( 
       
   124                     fs, 
       
   125                     iFilePath, 
       
   126                     EFileRead | EFileShareReadersOrWriters );
       
   127     // was there an error
       
   128     if( KErrNone != error )
       
   129         {
       
   130         GLX_LOG_INFO1( 
       
   131             "CShwMusicControl error in track %d", error );
       
   132         // let the observer know there was an error
       
   133         iMusicObsvr.ErrorWithTrackL( error );
       
   134         User::Leave( error );
       
   135         }
       
   136 
       
   137     CleanupClosePushL( file );
       
   138     iPlaybackUtility->InitL( file );
       
   139     CleanupStack::PopAndDestroy( &file );
       
   140     CleanupStack::PopAndDestroy( &fs );
       
   141 
       
   142     // The track should loop
       
   143     iPlaybackUtility->SetL( EPbPropertyRepeatMode, EPbRepeatOne );
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // VolumeL
       
   148 // Retrieve the volume values
       
   149 // -----------------------------------------------------------------------------
       
   150 void CShwMusicControl::VolumeL()
       
   151     {
       
   152     TRACER("CShwMusicControl::VolumeL");
       
   153     GLX_LOG_INFO( "CShwMusicControl::VolumeL" );
       
   154     // Retrieve the volume - Volume indicator is shown upon slideshow start and
       
   155     // when the volume is changed
       
   156     if (iMaxVolume == KErrNotFound)
       
   157         {
       
   158         iPlaybackUtility->ValueL( *this, EPbPropertyMaxVolume );	
       
   159         }
       
   160     iPlaybackUtility->ValueL( *this, EPbPropertyVolume );
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // NotifyL.
       
   165 // -----------------------------------------------------------------------------
       
   166 void CShwMusicControl::NotifyL( MShwEvent* aEvent )
       
   167     {
       
   168     TRACER("CShwMusicControl::NotifyL");
       
   169     GLX_LOG_INFO( "CShwMusicControl::NotifyL" );
       
   170     // only handle events if music initialisation succeeded
       
   171     if( iState == EMusicOn )
       
   172         {
       
   173         // reset state flag
       
   174         iSwitchingMusicOn = EFalse;
       
   175         // music playback is sacrificed if MPX fails
       
   176         TRAPD( errr, HandleEventL( aEvent ) );
       
   177         // if we had an error while switching music back on, tell observer 
       
   178         // that music is off
       
   179         if( ( errr != KErrNone )&&
       
   180             ( iSwitchingMusicOn ) )
       
   181             {
       
   182             iMusicObsvr.MusicOff();
       
   183             }
       
   184         }
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // From class MMPXPlaybackCallback.
       
   189 // Handle the change of status of playback engine
       
   190 // ---------------------------------------------------------------------------
       
   191 void CShwMusicControl::HandlePropertyL( TMPXPlaybackProperty aProperty,
       
   192                                         TInt aValue,
       
   193                                         TInt aError )
       
   194     {
       
   195     TRACER("CShwMusicControl::HandlePropertyL");
       
   196     GLX_LOG_INFO( "CShwMusicControl::HandlePropertyL" );
       
   197 
       
   198     // leave if there was an error
       
   199     User::LeaveIfError( aError );
       
   200 
       
   201     // handle max volume and volume, ignore other properties
       
   202     if( EPbPropertyMaxVolume == aProperty )
       
   203         {
       
   204         iMaxVolume = aValue;
       
   205         }
       
   206     else if( EPbPropertyVolume == aProperty )
       
   207         {
       
   208         // set the current volume
       
   209         iCurrentVolume = aValue;
       
   210         // call observer only when max volume is also known
       
   211         // iMaxVolume and iCurrentVolume are initialized to KErrNotFound
       
   212         if( iMaxVolume != KErrNotFound )
       
   213         	{
       
   214         	iMusicObsvr.MusicVolumeL( iCurrentVolume, iMaxVolume );
       
   215         	}
       
   216         }
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // From MMPXPlaybackCallback
       
   221 // Method is called continously until aComplete=ETrue, signifying that 
       
   222 // it is done and there will be no more callbacks
       
   223 // Only new items are passed each time
       
   224 // -----------------------------------------------------------------------------
       
   225 void CShwMusicControl::HandleSubPlayerNamesL(
       
   226     TUid /*aPlayer*/, const MDesCArray* /*aSubPlayers*/,
       
   227     TBool /*aComplete*/, TInt /*aError*/ )
       
   228     {
       
   229     }
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 // From class MMPXPlaybackCallback.
       
   233 // Call back of media request
       
   234 // ---------------------------------------------------------------------------
       
   235 void CShwMusicControl::HandleMediaL(
       
   236     const CMPXMedia& /*aProperties*/, TInt /*aError*/ )
       
   237     {
       
   238     }
       
   239 
       
   240 // ---------------------------------------------------------------------------
       
   241 // From class MMPXPlaybackObserver 
       
   242 // Handle playback message - required for debugging state of MPX 
       
   243 // Player component
       
   244 // ---------------------------------------------------------------------------
       
   245 void CShwMusicControl::HandlePlaybackMessageL(
       
   246     const TMPXPlaybackMessage& /*aMessage*/ )
       
   247     {
       
   248     }
       
   249 	
       
   250 // ---------------------------------------------------------------------------
       
   251 // From MMPXPlaybackObserver
       
   252 // ---------------------------------------------------------------------------
       
   253 void CShwMusicControl::HandlePlaybackMessage(const CMPXMessage& aMsg)
       
   254     {
       
   255     TRACER("CShwMusicControl::HandlePlaybackMessage");
       
   256     GLX_LOG_ENTRY_EXIT( "CShwMusicControl::HandlePlaybackMessageL()" );
       
   257     
       
   258     switch(*aMsg.Value<TMPXPlaybackMessage::TEvent>( KMPXMessageGeneralEvent ))
       
   259         {
       
   260         case TMPXPlaybackMessage::EInitializeComplete:
       
   261                 {
       
   262                 GLX_LOG_INFO( "TMPXPlaybackMessage::EInitializeComplete" );
       
   263                 // at here, can play the track
       
   264                 iCanPlay = ETrue;
       
   265 
       
   266                 //execute the cached "play" command 
       
   267                 if ( iPlayCached )
       
   268                     {
       
   269                     GLX_LOG_INFO( "iPlayCached" );
       
   270                     // Play the track
       
   271                     //The Code Scanner Error is not corrected here in the case of
       
   272                     //calling a leaving function in the Non Leaving Function
       
   273                     //We need to write a seperate Function for this.
       
   274                     //but will reduce only one error.
       
   275                     TRAP_IGNORE(iPlaybackUtility->CommandL( EPbCmdPlay ) );                    
       
   276 		            iMusicObsvr.MusicOnL();
       
   277 		            // need to call volume ourself to show the 
       
   278 		            // volume indicator in start
       
   279 		            VolumeL();                    
       
   280                     iPlayCached = EFalse;
       
   281                     }
       
   282                 break;
       
   283                 }
       
   284         default:            
       
   285            {
       
   286            GLX_LOG_INFO( "CShwMusicControl::HandlePlaybackMessage: Default" );
       
   287             break;
       
   288             }
       
   289         }  
       
   290     }
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // HandleEventL.
       
   294 // -----------------------------------------------------------------------------
       
   295 void CShwMusicControl::HandleEventL( MShwEvent* aEvent )
       
   296     {
       
   297     TRACER("CShwMusicControl::HandleEventL( MShwEvent* aEvent )");
       
   298     GLX_LOG_INFO( "CShwTimerControl::HandleEventL" );
       
   299     // we got an event, was it start
       
   300     if( dynamic_cast< TShwEventStart* >( aEvent ) )
       
   301         {
       
   302         GLX_LOG_INFO( "CShwMusicControl::NotifyL - TShwEventStart" );
       
   303         // set state flag
       
   304         iSwitchingMusicOn = ETrue;
       
   305         // iCanPlay is True when Player is Initialized.
       
   306         if ( iCanPlay )
       
   307             {
       
   308             GLX_LOG_INFO( "inside ICanPlay" );
       
   309             // Play the track
       
   310             TRAP_IGNORE(iPlaybackUtility->CommandL( EPbCmdPlay ) );
       
   311             iMusicObsvr.MusicOnL();
       
   312             // need to call volume ourself to show the 
       
   313             // volume indicator in start
       
   314             VolumeL();
       
   315             }
       
   316         else
       
   317             {
       
   318             GLX_LOG_INFO( "inside Else: ICanPlay" );
       
   319 			// initialization is not finished ,and cache the "play" command 
       
   320             iPlayCached = ETrue;
       
   321             }     
       
   322         }
       
   323     else if( dynamic_cast< TShwEventPause* >( aEvent ) )
       
   324         {
       
   325         GLX_LOG_INFO( "CShwMusicControl::NotifyL - TShwEventPause" );
       
   326         // pause playback
       
   327         iPlaybackUtility->CommandL( EPbCmdPause );
       
   328         // let the observer know
       
   329         iMusicObsvr.MusicOff();
       
   330         }
       
   331     else if( dynamic_cast< TShwEventResume* >( aEvent ) )
       
   332         {
       
   333         GLX_LOG_INFO( "CShwMusicControl::NotifyL - TShwEventResume" );
       
   334         // set state flag
       
   335         iSwitchingMusicOn = ETrue;
       
   336         // resume
       
   337         iPlaybackUtility->CommandL( EPbCmdPlay );
       
   338         // let the observer know
       
   339         iMusicObsvr.MusicOnL();
       
   340         }
       
   341     else if( dynamic_cast< TShwEventVolumeDown* >( aEvent ) )
       
   342         {
       
   343         GLX_LOG_INFO( "CShwMusicControl::NotifyL - TShwEventVolumeDown" );
       
   344 
       
   345         iPlaybackUtility->CommandL( EPbCmdDecreaseVolume );
       
   346         VolumeL();
       
   347         }
       
   348     else if( dynamic_cast< TShwEventVolumeUp* >( aEvent ) )
       
   349         {
       
   350         GLX_LOG_INFO( "CShwMusicControl::NotifyL - TShwEventVolumeUp" );
       
   351 
       
   352         iPlaybackUtility->CommandL( EPbCmdIncreaseVolume );
       
   353         VolumeL();
       
   354         }
       
   355     else if( dynamic_cast< TShwEventToggleControlUi* >( aEvent ) )
       
   356         {
       
   357         GLX_LOG_INFO( "CShwMusicControl::NotifyL - TShwEventToggleControlUi" );
       
   358         // Have to impliment if need comes
       
   359         }
       
   360     }
       
   361