Categories: Audio | Symbian C++ | S60 | How To | Code Examples
This page was last modified 11:20, 26 November 2007.
How to play sounds
From Forum Nokia Wiki
Simple sounds (or sound sequences) can be played using the following helper classes:
iSoundEngine = CSoundEngine::NewL(); // ... TTone tone; iSoundEngine->Reset(); tone.duration = 100; tone.pitch = 440; // frequency in Hz iSoundEngine->Append(tone); tone.pitch = 660; // frequency in Hz iSoundEngine->Append(tone); tone.pitch = 880; // frequency in Hz iSoundEngine->Append(tone); iSoundEngine->Play(); /////////////////////////////// // SoundEngine.h /////////////////////////////// /* Leszek Buczkowski, Ingraal 2006 */ #ifndef __SOUND_ENGINE_H__ #define __SOUND_ENGINE_H__ #include <e32base.h> #include <MdaAudioTonePlayer.h> #include "Delay.h" class TTone { public: TInt pitch; TInt duration; }; class CSoundEngine : public MMdaAudioToneObserver, MDelayObserver { public: static CSoundEngine* NewL(); virtual ~CSoundEngine(); void Reset(); void Append(TTone& aTone); void Play(); void Stop(); private: CSoundEngine(); void ConstructL(); void MatoPrepareComplete(TInt aError); void MatoPlayComplete(TInt aError); void DelayDoneL(); private: CMdaAudioToneUtility* iMdaAudioToneUtility; RArray<TTone> iTones; TInt iIndex; CDelay* iDelay; }; #endif /////////////////////////////// // SoundEngine.cpp /////////////////////////////// /* Leszek Buczkowski, Ingraal 2006 */ #include "SoundEngine.h" CSoundEngine* CSoundEngine::NewL() { CSoundEngine* self=new(ELeave) CSoundEngine(); CleanupStack::PushL(self); self->ConstructL(); CleanupStack::Pop(self); return self; } CSoundEngine::CSoundEngine() : iIndex(0) { } void CSoundEngine::ConstructL() { iMdaAudioToneUtility = CMdaAudioToneUtility::NewL(*this); iDelay = CDelay::NewL(*this); } CSoundEngine::~CSoundEngine() { delete iDelay; delete iMdaAudioToneUtility; } void CSoundEngine::MatoPrepareComplete(TInt /*aError*/) { iMdaAudioToneUtility->SetVolume(iMdaAudioToneUtility->MaxVolume()); } void CSoundEngine::MatoPlayComplete(TInt /*aError*/) { if (iIndex < iTones.Count()) { if (iTones[iIndex].pitch > 0) { TTimeIntervalMicroSeconds interval(1000 * iTones[iIndex].duration); iMdaAudioToneUtility->PrepareToPlayTone(iTones[iIndex].pitch, interval); iIndex++; iMdaAudioToneUtility->Play(); } else { iDelay->Delay(1000 * iTones[iIndex].duration); iIndex++; } } } void CSoundEngine::DelayDoneL() { MatoPlayComplete(0); } void CSoundEngine::Reset() { Stop(); iTones.Reset(); } void CSoundEngine::Append(TTone& aTone) { iTones.Append(aTone); } void CSoundEngine::Play() { Stop(); MatoPlayComplete(0); } void CSoundEngine::Stop() { iDelay->Cancel(); iMdaAudioToneUtility->CancelPlay(); iIndex = 0; } /////////////////////////////// // Delay.h /////////////////////////////// /* Leszek Buczkowski, Ingraal 2006 */ #ifndef __DELAY_H__ #define __DELAY_H__ #include <e32base.h> class MDelayObserver { public: virtual void DelayDoneL() = 0; }; class CDelay : public CActive { public: static CDelay* NewL(MDelayObserver &aObserver); ~CDelay(); void Delay(TTimeIntervalMicroSeconds32 aPeriod); private: CDelay(MDelayObserver &aObserver); void ConstructL(); void RunL(); void DoCancel(); private: RTimer iTimer; TTimeIntervalMicroSeconds32 iPeriod; MDelayObserver &iObserver; }; #endif /////////////////////////////// // Delay.cpp /////////////////////////////// /* Leszek Buczkowski, Ingraal 2006 */ #include "Delay.h" CDelay* CDelay::NewL(MDelayObserver &aObserver) { CDelay* self=new(ELeave) CDelay(aObserver); CleanupStack::PushL(self); self->ConstructL(); CleanupStack::Pop(self); return self; } CDelay::CDelay(MDelayObserver &aObserver): CActive(0), iPeriod(0), iObserver(aObserver) { CActiveScheduler::Add(this); } void CDelay::ConstructL() { User::LeaveIfError(iTimer.CreateLocal()); } CDelay::~CDelay() { Cancel(); iTimer.Close(); } void CDelay::Delay(TTimeIntervalMicroSeconds32 aPeriod) { iTimer.After(iStatus, aPeriod); SetActive(); void CDelay::RunL() { iObserver.DelayDoneL(); } void CDelay::DoCancel() { iTimer.Cancel(); }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A couple of questions about audio playing. | MePHyst0 | Symbian Media (Graphics & Sounds) | 3 | 2006-03-22 05:48 |
| sound sucks in real 7650 | cooler1 | Mobile Java General | 0 | 2002-08-09 08:31 |
| How can I learn if the phone is in the Manner/Silent mode? | Marat Khalili | Symbian Media (Graphics & Sounds) | 0 | 2005-09-12 05:10 |
| Digital sound (noobie!) | dhoskins | Mobile Java General | 1 | 2003-02-20 05:03 |
| 6280 startup sounds with theme studio | ahmedie | Tools and SDK Feedback | 0 | 2006-07-23 14:06 |
