android - SoundPool

 

在android中,可以使用SoundPool来实现游戏音效。使用SoundPool的优点是异步线程,占用资源少,可以同时合成多种音效,因而比较适合于在游戏中使用,比如打斗声,效果声,按钮声等。

用SoundPool载入和播放多个音效时,建议使用相同参数的声音文件(如位速,声道,采样率等),并且最好使用OGG格式,否则某些型号的手机可能会出现杂音,重复播放等问题。

int streamVolume;
 private SoundPool soundPool;
 private HashMap<Integer, Integer> soundPoolMap;
 Context ctx;

 public void initSounds(Context context)
 {   
  ctx = context;
  //初始化soundPool 对象,第一个参数是允许有多少个声音流同时播放,第2个参数是声音类型,第三个参数是声音的品质  
  soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);   

  //初始化HASH表  
  soundPoolMap = new HashMap<Integer, Integer>();   

  //获得声音设备和设备音量  
  AudioManager mgr = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);  
  streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);  
 }  

 public void loadSfx(int raw, int ID)
 {  
  //raw为声音资源名,ID为自己分配的识别号,播放时用此ID即可   
  soundPoolMap.put(ID, soundPool.load(ctx, raw, ID));   
 }  
 
 public void play(int ID, int uLoop)
 {        
  soundPool.play(soundPoolMap.get(ID), streamVolume, streamVolume, 0, uLoop, 1f);  
 }  
 
 public void replease()
 {
  soundPool.release();
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值