Main.cpp
Code:
/*
============================================================================
Name : Mp3Player.cpp
Author :
Copyright : Your copyright notice
Description : Exe source file
============================================================================
*/
// Include Files
#include "main.h"
#include <e32base.h>
#include <e32std.h>
#include <e32cons.h> // Console
#include <mda\common\audio.h>
#include <MdaAudioOutputStream.h>
#include <Player.h>
// Constants
_LIT(KTextConsoleTitle, "Console");
_LIT(KTextFailed, " failed, leave code = %d");
_LIT(KTextPressAnyKey, " [press any key]\n");
_LIT(KWait, "Waiting in main\n");
_LIT(KVolLevel,"Max Volume is %d\n");
_LIT(KFile,"C:\\astand.wav");
// Global Variables
LOCAL_D CConsoleBase* console; // write all messages to this
// Local Functions
LOCAL_C void MainL()
{
CPlayerUtility *player;
CSoundPlayer *player1;
TRAPD(err,player1=CSoundPlayer::NewL(_L("C:\\astand.wav")););
console->Write(_L("Hello, world!\n"));
console->Printf(KWait);
}
LOCAL_C void DoStartL()
{
// Create active scheduler (to run active objects)
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
CleanupStack::PushL(scheduler);
CActiveScheduler::Install(scheduler);
MainL();
// Delete active scheduler
CleanupStack::PopAndDestroy(scheduler);
}
// Global Functions
GLDEF_C TInt E32Main()
{
// Create cleanup stack
__UHEAP_MARK;
CTrapCleanup* cleanup = CTrapCleanup::New();
// Create output console
TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(
KConsFullScreen, KConsFullScreen)));
if (createError)
return createError;
// Run application code inside TRAP harness, wait keypress when terminated
TRAPD(mainError, DoStartL());
if (mainError)
console->Printf(KTextFailed, mainError);
console->Printf(KTextPressAnyKey);
console->Getch();
delete console;
delete cleanup;
__UHEAP_MARKEND;
return KErrNone;
}
player.h
Code:
ifndef PLAYER_H
#define PLAYER_H
#include <e32std.h>
#include <MdaAudioSamplePlayer.h>
class CPlayerUtility : public CBase, public MMdaAudioPlayerCallback
{
public:
static CPlayerUtility* NewL(const TDesC& aFileName);
static CPlayerUtility* NewLC(const TDesC& aFileName);
~CPlayerUtility();
private:
CPlayerUtility();
void ConstructL(const TDesC& aFileName);
public:
void Play();
void Stop();
public: // from MMdaAudioToneObserver
void MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& aDuration);
void MapcPlayComplete(TInt aError);
private:
CMdaAudioPlayerUtility* iPlayUtility;
TBool iPlaying,iPrepared;
};
Player.cpp
Code:
#include <MdaAudioTonePlayer.h>
#include <eikmenup.h>
#include "Player.h"
CPlayerUtility* CPlayerUtility::NewL(const TDesC& aFileName)
{
CPlayerUtility* self = NewLC(aFileName);
CleanupStack::Pop(self);
return self;
}
CPlayerUtility* CPlayerUtility::NewLC(const TDesC& aFileName)
{
CPlayerUtility* self = new (ELeave) CPlayerUtility();
CleanupStack::PushL(self);
self->ConstructL(aFileName);
return self;
}
CPlayerUtility::~CPlayerUtility()
{
if(iPlayUtility)
{
iPlayUtility->Stop();
iPlayUtility->Close();
}
delete iPlayUtility;
}
CPlayerUtility::CPlayerUtility()
{
}
void CPlayerUtility::ConstructL(const TDesC& aFileName)
{
iPlayUtility = CMdaAudioPlayerUtility::NewFilePlayerL(aFileName, *this);
iPlaying = iPrepared = EFalse;
}
void CPlayerUtility::Play()
{
iPlayUtility->Play();
iPlaying = ETrue;
}
void CPlayerUtility::Stop()
{
iPlayUtility->Stop();
iPlaying = EFalse;
}
void CPlayerUtility::MapcPlayComplete(TInt /*aError*/)
{
iPlaying = EFalse;
}
void CPlayerUtility::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& aDuration)
{
if(aError == KErrNone)
{
iPrepared = ETrue;
iPlayUtility->SetVolume(iPlayUtility->MaxVolume());
}
}