×
Namespaces

Variants
Actions
Revision as of 03:06, 22 February 2012 by hamishwillee (Talk | contribs)

Playing audio files using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

This article discusses an audio player utility class, its implementation and use.

Article Metadata
Article
Created: symbianyucca (22 Mar 2007)
Last edited: hamishwillee (22 Feb 2012)

Contents

The Utility Class

In general audio files can be played either with CMdaAudioRecorderUtility or with CMdaAudioPlayerUtility.

The CPlayerUtility illustrates how to play files with CMdaAudioPlayerUtility. It requires the MMdaAudioPlayerCallback interface to be implemented by the class in which file needs to be played. This interface has two methods. Method MapcInitComplete() is called when the file opening is completed. Once this function is called by the CMdaAudioPlayerUtility, the Play() function should be called.

The MapcPlayComplete method is called when playing of audio is completed. You can then restart playing the file by calling Play() function again.

If you would like to play buffers instead of files you can change the NewFilePlayerL() to be NewDesPlayerL() instead.

CAudioPlayer then illustrates file playing with CMdaAudioRecorderUtility. It requires MMdaObjectStateChangeObserver to be implemented. It is used to update different states of the file playing. File is ready to be played after this interface method is called with EOpen for the player object. Note that EOpen is also given when the audio playing finishes so you should also check the previous state variable. This example also requires calling class to implement its own callback interface method that is used for updating the playing status. This status is updated according to the players status as well the playing process is updated using timer implementation.

Source Code

AudioPlayer.cpp

#include <Mda\Common\Resource.h>
#include <BAUTILS.H>
 
 
CAudioPlayer* CAudioPlayer::NewL(MExamplePlayStateObserver& aObserver)
{
CAudioPlayer* self = CAudioPlayer::NewLC(aObserver);
CleanupStack::Pop(self);
return self;
}
 
CAudioPlayer* CAudioPlayer::NewLC(MExamplePlayStateObserver& aObserver)
{
CAudioPlayer* self = new (ELeave) CAudioPlayer(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
 
 
CAudioPlayer::CAudioPlayer(MExamplePlayStateObserver& aObserver)
:iObserver(aObserver),iVolume(5)
{
}
 
 
CAudioPlayer::~CAudioPlayer()
{
delete iExampleTimer;
 
if(iPlayerUtility)
{
iPlayerUtility->Stop();
iPlayerUtility->Close();
}
 
delete iPlayerUtility;
}
 
void CAudioPlayer::ConstructL()
{
iExampleTimer = CExampleTimer::NewL(CActive::EPriorityStandard,*this);
 
ReportStateAndTime();
}
 
void CAudioPlayer::TimerExpired(TAny* /*aTimer*/,TInt aError)
{
// update states first.
ReportStateAndTime();
 
if(iExampleTimer && aError != KErrCancel)
{
iExampleTimer->After(KReFreshTimeOut);
}
}
 
 
void CAudioPlayer::PlayL(const TDesC& aFileName)
{
iCurrentFile.iName.Copy(aFileName);
 
if(iExampleTimer)
{
iExampleTimer->Cancel();
}
 
if(iPlayerUtility)
{
iPlayerUtility->Stop(); // stop any play/rec
iPlayerUtility->Close();// close previously opened file.
}
 
delete iPlayerUtility; // and then we can delete it.
iPlayerUtility = NULL;
iPlayerUtility = CMdaAudioRecorderUtility::NewL(*this);
iPlayerUtility->OpenFileL(iCurrentFile.iName);
 
if(iExampleTimer)
{
iExampleTimer->After(KReFreshTimeOut);
}
}
 
void CAudioPlayer::StopL(void)
{
if(iExampleTimer)
{
iExampleTimer->Cancel();
}
 
if(iPlayerUtility)
{
iPlayerUtility->Stop();
}
 
ReportStateAndTime();
}
 
void CAudioPlayer::SetVolume(TInt& aVolume)
{
if(aVolume < 1)
aVolume = 1;
else if(aVolume > 10)
aVolume = 10;
 
iVolume = aVolume;// save to internal value always
if(iPlayerUtility) // and if utility exists, set it to it as well.
{
TInt Vol = ((iPlayerUtility->MaxVolume() * iVolume) / 10);
iPlayerUtility->SetVolume(Vol);
}
}
 
 
void CAudioPlayer::ReportStateAndTime(void)
{
TInt CurrPosition(0),FileDuration(0);
 
CMdaAudioClipUtility::TState CurrState(CMdaAudioClipUtility::ENotReady);
 
if(iPlayerUtility)
{
CurrState = iPlayerUtility->State();
 
TInt64 HelpPos = iPlayerUtility->Position().Int64();
// micro seconds, thus lets make it seconds for UIs
CurrPosition = HelpPos / 1000000;
 
// and with playing its the file duration.
HelpPos = iPlayerUtility->Duration().Int64();
FileDuration = HelpPos / 1000000;
}
 
// update valus to the UI
iObserver.StateUpdate(CurrState,CurrPosition,FileDuration);
}
 
 
void CAudioPlayer::MoscoStateChangeEvent(CBase* aObject, TInt aPreviousState, TInt aCurrentState, TInt /*aErrorCode*/)
{
if(aObject == iPlayerUtility)
{
ReportStateAndTime();
 
switch(aCurrentState)
{
case CMdaAudioClipUtility::EOpen:
{
// only when first opening files.
// also called with aCurrentState == EOpen, when
// playing or recording finishes.
if(aPreviousState == CMdaAudioClipUtility::ENotReady)
{
TInt Vol = ((iVolume * iPlayerUtility->MaxVolume()) / 10);
iPlayerUtility->SetVolume(Vol);
 
TRAPD(err,iPlayerUtility->PlayL(););
}
}
break;
case CMdaAudioClipUtility::EPlaying:
case CMdaAudioClipUtility::ERecording:
case CMdaAudioClipUtility::ENotReady:
default: // no need to do anything on these states.
break;
}
}
}


AudioPlayer.h

#include <MdaAudioSampleEditor.h>
#include <Mda\Client\Utility.h>
#include "CExampleTimer.h"
 
const TInt KReFreshTimeOut = 1000000; // re-fresh every second
//
class MExamplePlayStateObserver
{
public:
virtual void StateUpdate(CMdaAudioClipUtility::TState aState, TInt aPosition, TInt aDuration)=0;
};
 
 
class CAudioPlayer : public CBase, public MMdaObjectStateChangeObserver,MExampleTimerNotify
{
public:
static CAudioPlayer* NewL(MExamplePlayStateObserver& aObserver);
static CAudioPlayer* NewLC(MExamplePlayStateObserver& aObserver);
~CAudioPlayer();
public: // public functions
void PlayL(const TDesC& aFileName);
void StopL(void);
void SetVolume(TInt& aVolume);
protected: // from MMdaObjectStateChangeObserver & MExampleTimerNotify
void MoscoStateChangeEvent(CBase* aObject, TInt aPreviousState, TInt aCurrentState, TInt aErrorCode);
void TimerExpired(TAny* aTimer,TInt aError);
private:// interna functions
void ReportStateAndTime(void);
void ConstructL();
CAudioPlayer(MExamplePlayStateObserver& aObserver);
private:
MExamplePlayStateObserver& iObserver;
CMdaAudioRecorderUtility* iPlayerUtility;
TInt iVolume;
TMdaFileClipLocation iCurrentFile;
CExampleTimer* iExampleTimer;
};


PlayUtility.cpp

#include <MdaAudioTonePlayer.h>
#include <eikmenup.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());
iPlayUtility->Play();
}
}

PlayUtility.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;
};

About Emulator

In emulator this code may / maynot play files based on the situation of the sound drivers with emulator.

Links

90 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×