×
Namespaces

Variants
Actions

使用CAknKeySoundSystem自定义按键声音

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 17 Jun 2008 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

CS000953


  • 开发伙伴平台:

S60 3rd Edition, S60 3rd Edition, FP1


  • 详细描述

下列代码示例演示了如果使用CAknKeySoundSystem来自定义程序中按键的声音。这个示例通过定义自己的AVKON_SKEY_LIST和AVKON_SKEY_INFO结构来定义方向键的声音


此代码可以自签名执行


Contents

MMP文件

需要下列链接库

LIBRARY  avkon.lib
LIBRARY eikcore.lib
LIBRARY eiksrv.lib


资源文件

.hrh文件

#ifndef __TESTER_HRH__
#define __TESTER_HRH__
 
//...
 
//arrow sound ids
enum TArrowSoundId
{
EUpSoundId = 1,
EDownSoundId,
ELeftSoundId,
ERightSoundId
};
 
 
#endif // __TESTER_HRH__


.rss文件

#include <eikon.rh>
#include <avkon.rsg>
#include <avkon.rh>
 
#define KUpArrowSoundFile "c:\\data\\up.wav"
#define KDownArrowSoundFile "c:\\data\\down.wav"
#define KLeftArrowSoundFile "c:\\data\\left.wav"
#define KRightArrowSoundFile "c:\\data\\right.wav"
 
RESOURCE AVKON_SOUND_INFO_LIST r_arrow_sound_list
{
list =
{
// up arrow
AVKON_SOUND_INFO
{
sid = EUpSoundId;
priority = EAvkonKeyClickPriority;
preference = EAknAudioPrefDefaultTone;
file = KUpArrowSoundFile;
volume = 5;
},
// down arrow
AVKON_SOUND_INFO
{
sid = EDownSoundId;
priority = EAvkonKeyClickPriority;
preference = EAknAudioPrefDefaultTone;
file = KDownArrowSoundFile;
volume = 5;
},
// left arrow
AVKON_SOUND_INFO
{
sid = ELeftSoundId;
priority = EAvkonKeyClickPriority;
preference = EAknAudioPrefDefaultTone;
file = KLeftArrowSoundFile;
volume = 5;
},
// right arrow
AVKON_SOUND_INFO
{
sid = ERightSoundId;
priority = EAvkonKeyClickPriority;
preference = EAknAudioPrefDefaultTone;
file = KRightArrowSoundFile;
volume = 5;
}
};
}
 
RESOURCE AVKON_SKEY_LIST r_arrow_skey_list
{
list=
{
AVKON_SKEY_INFO
{
key = EStdKeyUpArrow;
sid = EUpSoundId;
},
AVKON_SKEY_INFO
{
key = EStdKeyDownArrow;
sid = EDownSoundId;
},
AVKON_SKEY_INFO
{
key = EStdKeyLeftArrow;
sid = ELeftSoundId;
},
AVKON_SKEY_INFO
{
key = EStdKeyRightArrow;
sid = ERightSoundId;
}
};
}


头文件

#ifndef __TESTERAPPVIEW_H__
#define __TESTERAPPVIEW_H__
 
// INCLUDES
#include <coecntrl.h>
 
class CAknKeySoundSystem;
 
class CTesterAppView : public CCoeControl
{
//...
private:
CAknKeySoundSystem* iSoundSystem;
TBool iCustomizedKeySounds;
};
 
#endif // __TESTERAPPVIEW_H__


源文件

#include <Tester_0xE0060BB5.RSG> //r_arrow_sound_list,r_arrow_skey_list
#include "Tester.hrh" //TArrowSoundId
#include <aknsoundsystem.h> // CAknKeySoundSystem
#include <aknappui.h>
 
void CTesterAppView::ConstructL(const TRect& aRect)
{
//...
iCustomizedKeySounds = EFalse;
iSoundSystem = static_cast<CAknAppUi*>(
CEikonEnv::Static()->AppUi() )->KeySounds();
 
if (!iSoundSystem)
{
//pointer to KeySound API object is null
User::Leave(KErrGeneral);
}
 
//install the application-specific set of sound ids
TRAPD( error, iSoundSystem->AddAppSoundInfoListL(
R_ARROW_SOUND_LIST ) );
 
 
if ((error != KErrNone) && (error != KErrAlreadyExists))
{
//install sound ids failed
User::Leave( error );
}
 
 
//NOTE: it is now also possible to play an installed sound
//just by calling the PlaySound() method e.g.
//iSoundSystem->PlaySound( EUpSoundId );
//...and stop playing it by calling
//iSoundSystem->StopSound( EUpSoundId );
 
}
 
CTesterAppView::~CTesterAppView()
{
iSoundSystem = NULL;
}
 
// ---------------------------------------------------------------------------
// Disable customized key sounds
// ---------------------------------------------------------------------------
//
void CTesterAppView::DisableCustomizedKeySounds()
{
if ( iSoundSystem && iCustomizedKeySounds )
{
//release the locked context
iSoundSystem->ReleaseContext();
 
//pop the pushed context from the context stack
iSoundSystem->PopContext();
 
iCustomizedKeySounds = EFalse;
}
}
 
 
// ---------------------------------------------------------------------------
// Enable customized key sounds
// ---------------------------------------------------------------------------
//
void CTesterAppView::EnableCustomizedKeySounds()
{
if ( iSoundSystem && !iCustomizedKeySounds )
{
//load key sound map from resources and push it to the context stack
iSoundSystem->PushContextL( R_ARROW_SKEY_LIST );
 
//tell server to use this context stack for processing sounds
iSoundSystem->BringToForeground();
 
//lock this context to the foreground, other BringToForeground() calls
//will be ignored until ReleaseContext() is called
iSoundSystem->LockContext();
 
iCustomizedKeySounds = ETrue;
}
}


如果EnableCustomizedKeySounds()方法被调用,用户按下方向键后就会播放自定义的键声音,如果调用DisableCustomizedKeySounds()方法,则将恢复默认声音的播放。

This page was last modified on 15 December 2011, at 09:26.
45 page views in the last 30 days.
×