Playing a WAV file
Posted on 4 March 2003 by Eric Bustarret
Playing a WAV file is easier as it may look at first sight since the OS does most of the work. In this project the class CSoundPlayer implements all the necessary stuff to do this:
#include <MdaAudioSamplePlayer.h>
class CSoundPlayer: public CBase, public MMdaAudioPlayerCallback
{
public:
static CSoundPlayer* NewL(const TDesC& aFile);
static CSoundPlayer* NewLC(const TDesC& aFile);
~CSoundPlayer();
void PlayL();
void StopL();
//
// from MMdaAudioPlayerCallback
//
void MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& aDuration);
void MapcPlayComplete(TInt aError);
private:
CSoundPlayer();
void ConstructL(const TDesC& aFile);
private:
enum TState
{
ENotReady,
EReady,
EPlaying
};
TState iState;
CMdaAudioPlayerUtility* iMdaPlayer;
};
The key classes are :
CMdaAudioPlayerUtility which implements the decoder. The class CSoundPlayer has a private member of this class called iMdaPlayer.
MMdaAudioPlayerCallback which is a kind of observer on iMdaPlayer. Basically this mixin class requires the implementation of the MapcInitComplete() and MapcPlayComplete() which will be described below.
Initialisation of the player
The player is initialised by calling CSoundPlayer::NewL() or CSoundPlayer::NewLC(). The second-phase constructor of the CSoundPlayer object will initialise the iMdaPlayer object using CMdaAudioPlayerUtility::NewFilePlayerL():
void CSoundPlayer::ConstructL(const TDesC& aFile)
{
//
// Create a file audio player utility instance
//
iMdaPlayer=CMdaAudioPlayerUtility::NewFilePlayerL(aFile,*this);
}
A second type of constructor, CMdaAudioPlayerUtility::NewDesPlayerL() is available if your WAV sample is already in RAM memory.
The player is not ready to play yet. Actually, if you try to call iMdaPlayer->PlayL() right after the NewFilePlayerL() call, you will probably hear nothing: you have to wait the player instance constuction to be ready to play. It will be signalled to you when the callback method MapcInitComplete() is called by the framework. Two typical implementation of this function are shown below:
//
// Implementation 1: set a iState flag to ready
// to reflect the fact that the player is ready
//
void CSoundPlayer::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
{
iState = aError ? ENotReady : EReady;
}
//
// Implementation 2: play the file immediately
//
void CSoundPlayer::MapcInitComplete(TInt aError, const TTimeIntervalMicroSeconds& /*aDuration*/)
{
if (!aError)
iMdaPlayer->PlayL();
}
Playing the file
Once the initialisation is complete, and as shown is the code above, the playback of the file is asked by a call to CMdaAudioPlayerUtility::PlayL(). In the Sound1 project, this is done by calling CSoundPlayer::PlayL():
void CSoundPlayer::Play()
{
if(iState==EReady)
{
iState=EPlaying;
iMdaPlayer->Play();
}
}
The framework will notify you the end of the playback by a call to MapcPlayComplete(). Here is the implementation for Sound1:
void CSoundPlayer::MapcPlayComplete(TInt aError)
{
iState = aError ? ENotReady : EReady;
}
In order to run this example, you must put a WAV file called play.wav in the directory C:\System\Apps\Sound\ of your device (C:\Symbian\6.1\Series60\Epoc32\Wins\c\system\apps\Sound under the simulator).
[
Last edited by lspo816 at 27-3-2007 16:38 ]