怎样实现在Android游戏音效控制
作者:孤风一剑 发布:2013-06-29 14:04 栏目:移动开发 点击:987次 评论关闭
- //音效的音量
- intstreamVolume;
- //定义SoundPool对象
- privateSoundPoolsoundPool;
- //定义HASH表
- privateHashMapsoundPoolMap;
- /***************************************************************
- *Function:initSounds();
- *Parameters:null
- *Returns:None.
- *Description:初始化声音系统
- *Notes:none.
- ***************************************************************/
- publicvoidinitSounds(){
- //初始化soundPool对象,第一个参数是允许有多少个声音流同时播放,第2个参数是声音类型,第三个参数是声音的品质
- soundPool=newSoundPool(100,AudioManager.STREAM_MUSIC,100);
- //初始化HASH表
- soundPoolMap=newHashMap();
- //获得声音设备和设备音量
- AudioManagermgr=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
- streamVolume=mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
- }
- /***************************************************************
- *Function:loadSfx();
- *Parameters:null
- *Returns:None.
- *Description:加载音效资源
- *Notes:none.
- ***************************************************************/
- publicvoidloadSfx(intraw,intID){
- //把资源中的音效加载到指定的ID(播放的时候就对应到这个ID播放就行了)
- soundPoolMap.put(ID,soundPool.load(context,raw,ID));
- }
- /***************************************************************
- *Function:play();
- *Parameters:sound:要播放的音效的ID,loop:循环次数
- *Returns:None.
- *Description:播放声音
- *Notes:none.
- ***************************************************************/
- publicvoidplay(intsound,intuLoop){
- soundPool.play(soundPoolMap.get(sound),streamVolume,streamVolume,1,uLoop,1f);
- }
游戏音效就是我们在玩游戏时出现的音乐,这个也是每个游戏必备的一部分,但有是你做游戏的背景音乐有间断的感觉的话,我们可以用 getCurrentPosition()这个方法来判断一下声音播放的偏移。其实这个也是非常简单的。只要我们在代码当中设置好(初始化声音)和(加载音效资源)就可以了,别的就和音乐播放器的代码差不多,像开始,停止。不多说了,我们还是先来看看代码当中是怎么实现音效的吧:
本文固定链接: http://www.devba.com/index.php/archives/1459.html | 开发吧