Symbian 震动及响铃功能

关于声音提示的使用

CoeSoundPlayer类使用

该类声明于coesndpy.h头文件,库是cone.lib,最简单的使用莫过于如下格式的代码应用

TBaSystemSoundType a(KSystemSoundMessageUID);

CoeSoundPlayer::PlaySoundNow(a);

在以上代码的使用时,第一行声明一个系统tone的类型,该类型声明在bassnd.h文件中,同时在mmp中加上bafl.lib库文件。通常这种简单应用,在模拟器上能够听到声音(3rd MR版本的模拟器上都听不到声音),但是在真机上,基本听不到声音,一个原因据说是默认的缺省音量被置成了KSystemSoundDefaultVolume,其值最大可以到100(我亲测的结果是最小为0,没有声音,最大只能到10,超过10之后和传负值一样都会报MMFAudioClient 4的错误,程序也会Crash。所以关于这点最好还是有高人指点下)。另外bassnd.h中定义的类型还有KSystemSoundRingUID, KSystemSoundAlarmUID, KUidSystemSoundError, KUidSystemSoundEvent等,具体的效果,可以自己亲测下。

稍微复杂一点的应用代码如下:

TBaSystemSoundType soundType(KSystemSoundMessageUID);

//TBaSystemSoundInfo::TTone soundTone(aFrequency, aDuration);

TBaSystemSoundInfo::TTone soundTone(1500, 3*1000*1000);

TBaSystemSoundInfo soundInfo(soundType, soundTone);

BaSystemSound::SetSoundL(CCoeEnv::Static()->FsSession(), soundInfo);

CoeSoundPlayer::PlaySoundNow(soundType);

在这里,我对音调不是很懂,但是aFrequency的值,经人测试100到3400是工作正常,效果很好(可能1000到3000最好), 3600到3800就变弱了,再往上到4000基本上已经听不到了。这种方法一般在真机上还是可以感受出来的,并非像第一种情况,只有模拟器上有效果。

只是长时间播放简单的音调估计很刺耳,那么我们就可以通过事先设计好的rng文件来进行播放单音铃声,具体代码如下:

_LIT(KRingToneFileName1, "\\Data\\Sounds\\simple\\alarm.rng");

const TInt KRingingTypeSilent = 4; // Silent

TInt tRingingType (0);

CRepository* tRepository = CRepository::NewLC(KCRUidProfileEngine);

User::LeaveIfError( tRepository ->Get(KProEngActiveRingingType, tRingingType ) );

if ( tRingingType != KRingingTypeSilent )

{

       TBaSystemSoundType soundType(KSystemSoundRingUID);

       TBaSystemSoundName soundName(KRingToneFileName1);

       CompleteWithAppPath(soundName);

       TBaSystemSoundInfo soundInfo(soundType, soundName, 10,
        KSystemSoundDefaultPriority);

       BaSystemSound::SetSoundL(CCoeEnv::Static()->FsSession(), soundInfo);

       CoeSoundPlayer::PlaySoundNow(soundType);

       //CoeSoundPlayer::PlaySound(soundType);

}

CleanupStack::PopAndDestroy(); // tRepository

使用以上代码需要注意的是alarm.rng文件必须要有,否则会没有声音传出,该文件在FP2版本的模拟器路径下没有,可以在S60_3rd_MR\Epoc32\release\winscw\udeb\z\system\sounds\simple下找到,并将其拷贝到相应的epoc32\release\winscw\udeb\z\system\sounds\simple下面即可。

注:另外,在这里虽然对情景模式是否静音进行了判断,其实不判断也是可以的,因为情景模式设为静音,仍然是可以播放出声音来的。这点很不同于震动。

震动控制API
设备, 软件 版本:
S60 2nd Edition, Nokia 6600, versions 3.42.1 and 4.09.1, and Nokia 7610, version 4.0421.4
说明:
S60 2nd Edition,S60 2nd Edition FP1,即Symbian OS v7.x不提供震动接口; S60 2nd Edition FP2,S60 2nd Edition FP3,即Symbian OS v8.x开始使用CVibraControl类提供震动接口; S60 3rd Edition, 即Symbian OS v9.x开始使用新的CHWRMVibra类提供震动接口。
详细描述:CVibraControl API已经在S60 SDK 2.0中有所描述,开发者也可以使用其工作。不过在Nokia6600中并没有被支持。 S60第二版,FP2和FP3设备已经包括了VibraCtrl.dll和震动功能。 S60第三版中提供了一个新的Vibra Client API(CHWRMVibra)
示例// for S60 3rd#include <hwrmvibra.h>  // CHWRMVibra, HWRMVibraClient.lib 

void DoVibrateL( TInt aDuration )

{  

  CHWRMVibra* vibra = CHWRMVibra::NewLC();   

  if ( CHWRMVibra::EVibraModeON == vibra->VibraSettings() )  // get vibration setting in the user profile 

  {    

    vibra->StartVibraL( aDuration );  

  }

  CleanupStack::PopAndDestroy( vibra );

}

// for S60 2nd FP2 and FP3#include <vibractrl.h>  // CVibraControl, VibraCtrl.lib 

void DoVibrateL( TUint16 aDuration )

{  

  CVibraControl* control = VibraFactory::NewL();   

  if ( CVibraControl::EVibraModeON == control->VibraSettings() )  // get vibration setting in the user profile 

  {    

    control->StartVibra( aDuration );  

  }   

  delete control;  

  control = NULL;

}

例子:

        #include <hwrmvibra.h>
        #include <coesndpy.h>
        #include <bassnd.h>
        #include <centralrepository.h>
	CHWRMVibra* vibra = CHWRMVibra::NewLC();	 
	if ( CHWRMVibra::EVibraModeON == vibra->VibraSettings() )  
       // get vibration setting in the user profile
	{
		vibra->StartVibraL( 30000 );
	}
	
	TFileName ringFileName;
	CUcgpsAppUi* pApp = (CUcgpsAppUi*) CEikonEnv::Static()->AppUi();
	pApp->GetPath(ringFileName);
	ringFileName.Append(_L("phonering.rng"));
	const TInt KRingingTypeSilent = 4; // Silent
	TInt tRingingType(0);
	CRepository* tRepository = CRepository::NewLC(KCRUidProfileEngine);
	User::LeaveIfError(
			tRepository ->Get(KProEngActiveRingingType, tRingingType));
	TBaSystemSoundType soundType(KSystemSoundRingUID);
	TBaSystemSoundName soundName(ringFileName);
	CompleteWithAppPath(soundName);
	TBaSystemSoundInfo soundInfo(soundType, soundName, 10,
				KSystemSoundDefaultPriority);
	BaSystemSound::SetSoundL(CCoeEnv::Static()->FsSession(), soundInfo);
	CoeSoundPlayer::PlaySoundNow(soundType, 10, 0);
	CleanupStack::PopAndDestroy(); // tRepository
	
	if (CSIPExLogView::RunQry_accept_callL() == EAknSoftkeyYes)
	{
		CleanupStack::PopAndDestroy( vibra );
		CoeSoundPlayer::CancelSound(soundType);
	}
	else
	{
		CleanupStack::PopAndDestroy( vibra );
		CoeSoundPlayer::CancelSound(soundType);
	}
原文地址:https://www.cnblogs.com/hummersofdie/p/1955181.html