/*
 ============================================================================
 Name		 : AudioPlayerEngine.cpp
 Copyright   : Symbian Academy, 2007
 Description : The audio player engine handles the communication with the
               multimedia framework.
 ============================================================================
 */

#include "AudioPlayerEngine.h"

CAudioPlayerEngine::CAudioPlayerEngine() :
	iState(EIdle)
	{
	// No implementation required
	}

CAudioPlayerEngine::~CAudioPlayerEngine()
	{
	// Edit 13: Call the Cleanup() method to stop the player if
	//          it's currently active and to delete the instance
	//          if necessary.
	
	}

CAudioPlayerEngine* CAudioPlayerEngine::NewLC()
	{
	CAudioPlayerEngine* self = new (ELeave)CAudioPlayerEngine();
	CleanupStack::PushL (self);
	self->ConstructL ();
	return self;
	}

CAudioPlayerEngine* CAudioPlayerEngine::NewL()
	{
	CAudioPlayerEngine* self=CAudioPlayerEngine::NewLC ();
	CleanupStack::Pop (); // self;
	return self;
	}

void CAudioPlayerEngine::ConstructL()
	{

	}

void CAudioPlayerEngine::PlayFileL(const TDesC &aFile)
	{
	// Only accept to play a new file if the current status is idle.
	if ( iState == EIdle)
		{
		// Playing a file has to be prepared first.
		iState = EPlayFilePrepare;
		// Edit 7: Initialize the audio player utility by calling
		//         CMdaAudioPlayerUtility::NewFilePlayerL().
		//         Pass the descriptor as a parameter and specify this
		//         class to get the call-backs.
		//         Doing this checks if the multimedia framework has a plug-in 
		//         that can handle the specified file type and initializes the 
		//         player. When this process is done (successfully or with an error),
		//         the MapcInitComplete()-method of the engine class will be executed.
		
		}
	}

void CAudioPlayerEngine::StopPlay()
	{
	// Edit 14: Call the cleanup function also in case the user stops playback.
	iState = EIdle;
	}

// Edit 8b: Implement the function void Cleanup().
//          In case the iAudioPlayer exists, this function should first
//          make sure playback is stopped by calling Stop() and then Close()
//          on the iAudioPlayer.
//          Next, delete the audio player object and set the pointer to NULL.
//          Outside of the if statement, set the state of the engine to EIdle.


TBool CAudioPlayerEngine::IsPlaying()
	{
	return (iState != EIdle);
	}

// Edit 5b: Implement the MapcInitComplete() callback method of 
//          the interface and leave it empty for now.


	// Edit 9: If aError is set to something different to KErrNone,
	//         call the Cleanup() method of the engine and leave this
	//         method instantly. Normally, you should inform the
	//         user about the problem, but this would be UI development
	//         and is out of scope of this exercise. Therefore, any
	//         error just cancels playback.

	
	// Edit 10: Set the internal state to EPlayFilePlaying

	// Edit 11: Call the Play() method of the audio player utility
	//          to start playback of the file.



// Edit 5c: Implement the MapcPlayComplete() callback method of 
//          the interface and leave it empty for now.
//          This function will be called when playback has completed.


	// Edit 12: When playback is complete, call the Cleanup()
	//          function to stop and free the resources.
	//          This method should also handle any errors, for
	//          example underflow if the emulator wasn't fast
	//          enough. However, this is not required for this
	//          exercise.



