Symbian
Symbian OS Library

FAQ-0519 How can I easily play a sound using EIKON?

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



 

Classification: C++ Category: EIKON
Created: 09/12/2000 Modified: 12/02/2001
Number: FAQ-0519
Platform: ER5

Question:
How can I easily play a sound using EIKON?

Answer:
There are several ways to play sounds in EPOC.

The first approach and more difficult is to use the CSoundPlayer class, and RDevSound device. The RDevSound device must be configured before any calls can be made to the CSoundPlayer methods to play a sound. Usually you will also have to create a new class which implements the mixin class MSoundPlayerObserver, and which will be notified when a sound has completed playing.

An example of the use of CSoundPlayer can be seen in the Boss puzzle \boss\eik8.

A far easier way to play a short sound is to make use of the Eikon class CEikAlarmSoundPlayer. This class is at a higher level than CSoundPlayer and thus hides a great deal of the complexity involved in playing a sound.

To use the CEikAlarmSoundPlayer class to play a short sound a suggested approach is as follows :

1. Add a CEikAlarmSoundPlayer member variable to a class within your app.
      class CExampleClass
      {
      ...
          private :
              // Private class member
              CEikAlarmSoundPlayer* iPlayer; ... }
              2. Construct the CEikAlarmSoundPlayer in the ConstructL() method of the class.

              void CExampleClass::ConstructL()
                {
                ...
                iPlayer = CEikAlarmSoundPlayer::NewL();
                ...
                }
                3. Play the sound.

                void CExampleClass::PlayASound()
                  {
                  _LIT(KSound, "Chimes");
                  TName name(KSound);
                  iPlayer->Play(CEikonEnv::Static()->FsSession(), name);
                  }
                  4. Remember to delete the CEikAlarmSoundPlayer in the class Destructor.

                  CExampleClass::~CExampleClass()
                    {
                    ...
                    delete iPlayer;
                    ...
                    }
                    The above example will play a file called "Chimes.snd" in \System\Alarms\.

                    Only delete the CEikAlarmSoundPlayer in the destructor or when the sound has definitely finished playing, as the CEikAlarmSoundPlayer creates a session with RDevSound for you. Deleting the object closes the session and playing any more sounds will not be possible. Also remember that a call to
                    the CEikAlarmSoundPlayer::Play() method is asynchronous, and it is possible the sound will not play immediately following the call. Deleting the CEikAlarmSoundPlayer object at this time will most likely result in the RDevSound session being closed before the sound has had a chance to be played.