I found a suggestion which is written in Nokia Known Issues document is very dangerous. You will find there, if you want easy way how to play camera click sound you can use calss CAknKeySoundSystem:


CAknKeySoundSystem* iSoundPlayer;

iSoundPlayer = (STATIC_CAST(CAknAppUi*,CEikonEnv::Static()->AppUi()))->KeySounds();

iSoundPlayer->AddAppSoundInfoListL( R_CAMERA_SNAP_SOUND );

iSoundPlayer->PlaySound( KSnapSoundId );


I found every call to AddAppSoundInfoListL (every start of your application) will add somewhere to Key Sound Stack information from R_CAMERA_SNAP_SOUND and allocates memory. It will be still allocates if you close your app. If you will run it about 20 times, you will get "YourApp: Memory Full. Close some applications and try again" message.

So, what is the correct solution?

If you will check aknsoundsystem.h file, you will find some functions which are not well documented. I though void PushContextL(R_CAMERA_SNAP_SOUND) should be the best, then Play and of course PopContext. But using this I was unable to play a camera click sound (or any other wav defined in your ressource file).

My solution is:

// init new instance
iSoundPlayer = CAknKeySoundSystem::NewL(0);

// add sound info (f.e in constructor)
iSoundPlayer->LockContext();
iSoundPlayer->AddAppSoundInfoListL( R_CAMERA_SNAP_SOUND );

// play camera sound
iSoundPlayer->PlaySound( KSnapSoundId );

// in destructor
iSoundPlayer->ReleaseContext();
delete iSoundPlayer;

If you don't use LockContext and ReleaseContext memory will be still allocated after delete iSoundPlayer too!!!

If anybody knows better solution how to do it using CAknKeySoundSystem (not using media player), please put your code here.