当前位置: 移动技术网 > 移动技术>移动开发>Android > 安卓(Android)游戏开发音效代码

安卓(Android)游戏开发音效代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

游戏音效就是我们在玩游戏时出现的音乐,这个也是每个游戏必备的一部分,但有是你做游戏的背景音乐有间断的感觉的话,我们可以用getcurrentposition()这个方法来判断一下声音播放的偏移。其实这个也是非常简单的。只要我们在代码当中设置好(初始化声音)和(加载音效资源)就可以了,别的就和音乐播放器的代码差不多,像开始,停止。不多说了,我们还是先来看看代码当中是怎么实现音效的吧:

1.音效的音量

int streamvolume;

//定义soundpool 对象 private soundpool soundpool;
  //定义hash表 private hashmap soundpoolmap;
  /*parameters: null
  * returns: none.
  * description: 初始化声音系统
  * notes: none.
  ***************************************************************/

2.初始化soundpool

public void initsounds() { //初始化soundpool 对象,第一个参数是允许有多少个声音流同时播放,第2个参数是声音类型,第三个参数是声音的品质 soundpool = new soundpool(100, audiomanager.stream_music, 100);
  //初始化hash表 soundpoolmap = new hashmap();
  //获得声音设备和设备音量 audiomanager mgr = (audiomanager)context.getsystemservice(context.audio_service);
  streamvolume = mgr.getstreamvolume(audiomanager.stream_music);
  }
  /*************************************************************** * function: loadsfx();
  * parameters: null
  * returns: none.
  * description: 加载音效资源
  * notes: none.
  ***************************************************************/

3.加载

public void loadsfx(int raw, int id) { //把资源中的音效加载到指定的id(播放的时候就对应到这个id播放就行了) soundpoolmap.put(id, soundpool.load(context, raw, id));
  }
  /*************************************************************** * function: play();
  * parameters: sound:要播放的音效的id, loop:循环次数
  * returns: none.
  * description: 播放声音
  * notes: none.
  ***************************************************************/
  public void play(int sound, int uloop) { soundpool.play(soundpoolmap.get(sound), streamvolume, streamvolume, 1, uloop, 1f); }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网