commands/play/play.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // play.cpp
       
     2 // 
       
     3 // Copyright (c) 2005 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 
       
    14 #include <e32base.h>
       
    15 #include <bacline.h>
       
    16 #include <mmf/server/sounddevice.h>
       
    17 #include "play.h"
       
    18 
       
    19 _LIT(KNewLine, "\n");
       
    20 
       
    21 
       
    22 //______________________________________________________________________________
       
    23 //						~CCmdPlay
       
    24 CCommandBase* CCmdPlay::NewLC()
       
    25 	{
       
    26 	CCmdPlay* self = new(ELeave)CCmdPlay();
       
    27 	CleanupStack::PushL(self);
       
    28 	self->ConstructL();
       
    29 	return self;
       
    30 	}
       
    31 	
       
    32 void CCmdPlay::ConstructL()
       
    33 	{
       
    34 	BaseConstructL();
       
    35 	iPlayer = CMmfPlayer::NewL(Stdout());
       
    36 	}
       
    37 	
       
    38 CCmdPlay::~CCmdPlay()
       
    39 	{
       
    40 	delete iPlayer;
       
    41 	}
       
    42 	
       
    43 const TDesC& CCmdPlay::Name() const
       
    44 	{
       
    45 	_LIT(KName, "play");
       
    46 	return KName;
       
    47 	}
       
    48 	
       
    49 void CCmdPlay::DoRunL()
       
    50 	{
       
    51 	if (iMaxVolume)
       
    52 		{
       
    53 		// we use DevSound to get the max volume, as CMdaAudioRecorderUtility
       
    54 		// always returns 0 until we have opened a file.
       
    55 		CMMFDevSound* devSound = CMMFDevSound::NewL();
       
    56 		TInt maxVol = devSound->MaxVolume();
       
    57 		TBool cons = Stdout().AttachedToConsole();
       
    58 		if (cons) Printf(_L("Maximum volume: "));
       
    59 		Printf(_L("%d"), maxVol);
       
    60 		if (cons) Printf(_L("\r\n"));
       
    61 		delete devSound;
       
    62 		}
       
    63 	else
       
    64 		{
       
    65 		if (!iArguments.IsPresent(0)) 
       
    66 			{
       
    67 			PrintError(KErrArgument, _L("No filename specified"));
       
    68 			DisplayHelp();
       
    69 			Complete(KErrArgument);
       
    70 			return;
       
    71 			}
       
    72 		iPlayer->GetReady(iFile, iVerbose);
       
    73 		if (iOptions.IsPresent(&iPriority) || iOptions.IsPresent(&iPreference))
       
    74 			{
       
    75 			iPlayer->SetPriority(iPriority, iPreference);
       
    76 			}
       
    77 		if (iOptions.IsPresent(&iVolume))
       
    78 			{
       
    79 			iPlayer->SetVolume(iVolume);
       
    80 			}
       
    81 		CActiveScheduler::Start();
       
    82 		User::LeaveIfError(iPlayer->Error());
       
    83 		}
       
    84 	}
       
    85 
       
    86 void CCmdPlay::OptionsL(RCommandOptionList& aOptions)
       
    87 	{
       
    88 	_LIT(KOptPriority, "priority");
       
    89 	aOptions.AppendIntL(iPriority, KOptPriority);
       
    90 
       
    91 	_LIT(KOptPreference, "preference");
       
    92 	aOptions.AppendIntL(iPreference, KOptPreference);
       
    93 
       
    94 	_LIT(KOptVolume, "volume");
       
    95 	aOptions.AppendIntL(iVolume, KOptVolume);
       
    96 
       
    97 	_LIT(KOptVerbose, "verbose");
       
    98 	aOptions.AppendBoolL(iVerbose, KOptVerbose);
       
    99 
       
   100 	_LIT(KOptMaxVolume, "max-volume");
       
   101 	aOptions.AppendBoolL(iMaxVolume, KOptMaxVolume);
       
   102 	}
       
   103 	
       
   104 void CCmdPlay::ArgumentsL(RCommandArgumentList& aArguments)
       
   105 	{
       
   106 	_LIT(KArgFiles, "file_name");
       
   107 	aArguments.AppendFileNameL(iFile, KArgFiles);
       
   108 	}
       
   109 			
       
   110 CCmdPlay::CCmdPlay()
       
   111 	{
       
   112 	}
       
   113 
       
   114 //______________________________________________________________________________
       
   115 //						CMmfPlayer
       
   116 CMmfPlayer* CMmfPlayer::NewL(RIoConsoleWriteHandle& aStdOut)
       
   117 	{
       
   118 	CMmfPlayer* self = new(ELeave)CMmfPlayer(aStdOut);
       
   119 	CleanupStack::PushL(self);
       
   120 	self->ConstructL();
       
   121 	CleanupStack::Pop(self);
       
   122 	return self;
       
   123 	}
       
   124 	
       
   125 void CMmfPlayer::ConstructL()
       
   126 	{
       
   127 	CActiveScheduler::Add(this);
       
   128 	iUtil = CMdaAudioRecorderUtility::NewL(*this);
       
   129 	}
       
   130 	
       
   131 CMmfPlayer::CMmfPlayer(RIoConsoleWriteHandle& aStdOut)
       
   132 	: CActive(CActive::EPriorityStandard), iStdOut(aStdOut), iVolume(-1)
       
   133 	{
       
   134 	}
       
   135 
       
   136 CMmfPlayer::~CMmfPlayer()
       
   137 	{
       
   138 	delete iUtil;
       
   139 	}
       
   140 	
       
   141 TInt CMmfPlayer::GetMaxVolume()
       
   142 	{
       
   143 	return iUtil->MaxVolume();
       
   144 	}
       
   145 	
       
   146 TInt CMmfPlayer::Error()
       
   147 	{
       
   148 	return iError;
       
   149 	}
       
   150 
       
   151 void CMmfPlayer::GetReady(const TDesC& aFileName, TBool aVerbose)
       
   152 	{
       
   153 	iFileName = &aFileName;
       
   154 	iVerbose = aVerbose;
       
   155 	iState = EInitial;
       
   156 	TRequestStatus* stat = &iStatus;
       
   157 	User::RequestComplete(stat, KErrNone);
       
   158 	SetActive();
       
   159 	}
       
   160 	
       
   161 void CMmfPlayer::SetPriority(TInt aPriority, TInt aPreference)
       
   162 	{
       
   163 	iUtil->SetPriority(aPriority, (TMdaPriorityPreference)aPreference);
       
   164 	}
       
   165 
       
   166 void CMmfPlayer::SetVolume(TInt aVolume)
       
   167 	{
       
   168 	iVolume = aVolume;
       
   169 	}
       
   170 
       
   171 void CMmfPlayer::RunL()
       
   172 	{
       
   173 	ProcessL();
       
   174 	}
       
   175 	
       
   176 TInt CMmfPlayer::RunError(TInt aError)
       
   177 	{
       
   178 	Error(aError);
       
   179 	return KErrNone;
       
   180 	}
       
   181 
       
   182 void CMmfPlayer::DoCancel()
       
   183 	{
       
   184 	}
       
   185 
       
   186 void CMmfPlayer::MoscoStateChangeEvent(CBase* /*aObject*/, TInt /*aPreviousState*/, TInt aCurrentState, TInt aErrorCode)
       
   187 	{
       
   188 	if (aErrorCode!=KErrNone)
       
   189 		{
       
   190 		Error(aErrorCode);
       
   191 		}
       
   192 	else
       
   193 		{
       
   194 		if (aCurrentState!=EPlaying)
       
   195 			{
       
   196 			TRAPD(err, ProcessL());
       
   197 			if (err!=KErrNone) Error(err);
       
   198 			}				
       
   199 		}
       
   200 	}
       
   201 	
       
   202 void CMmfPlayer::ProcessL()
       
   203 	{
       
   204 	switch (iState)
       
   205 		{
       
   206 	case EInitial:
       
   207 		iState = EOpening;
       
   208 		iUtil->OpenFileL(*iFileName);
       
   209 		Message(_L("Opening clip %S"), &iFileName);
       
   210 		break;
       
   211 	case EOpening:
       
   212 		iState = EPlaying;
       
   213 		if (iVolume != -1)
       
   214 			{
       
   215 			iUtil->SetVolume(iVolume);
       
   216 			}
       
   217 		iUtil->PlayL();
       
   218 		Message(_L("Playing"));
       
   219 		break;
       
   220 	case EPlaying:
       
   221 		Message(_L("Playing complete"));
       
   222 		Stop();
       
   223 		break;
       
   224 		}
       
   225 	}
       
   226 	
       
   227 void CMmfPlayer::Error(TInt aError)
       
   228 	{
       
   229 	switch (iState)
       
   230 		{
       
   231 	case EInitial:
       
   232 		Message(_L("Error %d"), aError);
       
   233 		break;
       
   234 	case EOpening:
       
   235 		Message(_L("Error %d when opening clip"), aError);
       
   236 		break;
       
   237 	case EPlaying:
       
   238 		Message(_L("Error %d when playing clip"), aError);
       
   239 		break;
       
   240 	default:
       
   241 		Message(_L("Error %d (in unknown state)"), aError);
       
   242 		break;
       
   243 		}
       
   244 	Stop();
       
   245 	iError = aError;
       
   246 	}
       
   247 	
       
   248 void CMmfPlayer::Message(TRefByValue<const TDesC> aFmt, ...)
       
   249 	{
       
   250 	if (iVerbose)
       
   251 		{
       
   252 		TDesC fmt(aFmt);
       
   253 		RBuf16 newText;
       
   254 		iMsgBuf.Zero();
       
   255 
       
   256 		TTruncateOverflow overflow;
       
   257 		VA_LIST list;
       
   258 		VA_START(list, aFmt);
       
   259 		iMsgBuf.AppendFormatList(aFmt, list, &overflow);
       
   260 		
       
   261 		iStdOut.Write(iMsgBuf);
       
   262 		iStdOut.Write(KNewLine);
       
   263 		}
       
   264 	}
       
   265 	
       
   266 void CMmfPlayer::Stop()
       
   267 	{
       
   268 	CActiveScheduler::Stop();
       
   269 	}
       
   270 
       
   271 EXE_BOILER_PLATE(CCmdPlay);
       
   272