Symbian
Symbian OS Library

FAQ-0892 How can I easily implement beeps in my app?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: Applications
Created: 07/09/2003 Modified: 10/16/2003
Number: FAQ-0892
Platform: Symbian OS v6.0, Symbian OS v6.1, Symbian OS v7.0, Symbian OS v7.0s

Question:
CEikonEnv::Beep() is deprecated. In order to play beeps you have to use CMdaAudioToneUtility
However this is a bit fiddly. Is there an easier way?


Answer:
From Symbian OS v6.1, CEikonEnv::Beep() and User::Beep() are deprecated.

    CBeep, below, shows the implementation of a simple wrapper around CMdaAudioToneUtility that you can use instead.
    The class allows you to create a CBeep with a duration and a frequency like this:

    TTimeIntervalMicroSeconds d1(500000);
    iBeep1=CBeep::NewL(440, d1);
    and then produce a beep by calling
    iBeep1->Play();

    You can create more than one of these objects, each with a different tone & duration, and call Play() as required.

    The class definition and implementation are provided below:

    class CBeep : public CBase, public MMdaAudioToneObserver
    {
    public:
    static CBeep* NewL(TInt aFrequency,  TTimeIntervalMicroSeconds iDuration);
    void Play();
    ~CBeep();
    private:
    void ConstructL(TInt aFrequency,  TTimeIntervalMicroSeconds iDuration);
    void MatoPrepareComplete(TInt aError);
    void MatoPlayComplete(TInt aError);
    private:
    typedef enum TBeepState
    {
    EBeepNotPrepared,
    EBeepPrepared,
    EBeepPlaying
    };
    private: CMdaAudioToneUtility* iToneUtil;
    TBeepState iState;
    TInt iFrequency;
    TTimeIntervalMicroSeconds iDuration;
    };

    CBeep::~CBeep()
    {
    delete iToneUtil;
    }

    CBeep* CBeep::NewL(TInt aFrequency, TTimeIntervalMicroSeconds aDuration)
    {
    CBeep* self=new (ELeave) CBeep();
    CleanupStack::PushL(self);
    self->ConstructL(aFrequency, aDuration);
    CleanupStack::Pop();
    return self;
    };

    void CBeep::ConstructL(TInt aFrequency, TTimeIntervalMicroSeconds aDuration)
    {
    iToneUtil=CMdaAudioToneUtility::NewL(*this);
    iState=EBeepNotPrepared;
    iFrequency=aFrequency;
    iDuration=aDuration;
    iToneUtil->PrepareToPlayTone(iFrequency,iDuration);
    }

    void CBeep::Play()
    {
    if (iState!=EBeepNotPrepared)
    {
    if (iState==EBeepPlaying)
    {
    iToneUtil->CancelPlay();
    iState=EBeepPrepared;
    }
    }
    iToneUtil->Play();
    iState=EBeepPlaying;
    }

    void CBeep::MatoPrepareComplete(TInt aError)
    {
    if (aError==KErrNone)
    iState=EBeepPrepared;
    }

    void CBeep::MatoPlayComplete(TInt aError)
    {
    iState=EBeepPrepared;
    }