Playing audio tones
Article Metadata
The CTonePlayer example illustrates how to play tones on Symbian devices. This example plays a simple constant tone for a predefined time, but CMdaAudioToneUtility could also be used to play DTMF (Dual-Tone Multi-Frequency) strings and tone sequences.
To use CMdaAudioToneUtility you need to implement MMdaAudioToneObserver callback interface, which is used by the player to update its status. MMdaAudioToneObserver has two methods defined from which MatoPrepareComplete is called when the tone player is initialized and ready to play. Play function should never be called before MatoPrepareComplete is called. The MatoPlayComplete method is called after the playing of the tone has finalized.
TonePlayer.cpp
#include <MdaAudioTonePlayer.h>
#include <eikmenup.h>
CTonePlayer* CTonePlayer::NewL()
{
CTonePlayer* self = NewLC();
CleanupStack::Pop(self);
return self;
}
CTonePlayer* CTonePlayer::NewLC()
{
CTonePlayer* self = new (ELeave) CTonePlayer();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CTonePlayer::CTonePlayer(): iFrequency(125),iDuration(5000000)
{
}
CTonePlayer::~CTonePlayer()
{
delete iToneUtility;
}
void CTonePlayer::ConstructL()
{
iToneUtility = CMdaAudioToneUtility::NewL(*this);
iToneUtility->PrepareToPlayTone(iFrequency,iDuration);
}
void CTonePlayer::Play()
{
iToneUtility->Play();
}
void CTonePlayer::Stop()
{
iToneUtility->CancelPlay();
}
void CTonePlayer::MatoPrepareComplete(TInt /*aError*/)
{
iToneUtility->SetVolume(iToneUtility->MaxVolume());
}
void CTonePlayer::MatoPlayComplete(TInt /*aError*/)
{
}
TonePlayer.h
#include <e32std.h>
#include <MdaAudioTonePlayer.h>
class CTonePlayer : public CBase, public MMdaAudioToneObserver
{
public:
static CTonePlayer* NewL();
static CTonePlayer* NewLC();
~CTonePlayer();
public:
void Play();
void Stop();
protected: // from MMdaAudioToneObserver
void MatoPrepareComplete(TInt aError);
void MatoPlayComplete(TInt aError);
private:
CTonePlayer();
void ConstructL();
private:
CMdaAudioToneUtility* iToneUtility;
TInt iFrequency;
TTimeIntervalMicroSeconds iDuration;
};


(no comments yet)