Android 即时音效

来源:互联网 发布:erp基础数据的作用 编辑:程序博客网 时间:2024/05/16 08:43
 

 SoundPool sp;
   HashMap<Integer,Integer> spMap;

 public void initSoundPool(){
     sp=new SoundPool(5,AudioManager.STREAM_MUSIC,0);
     spMap=new HashMap<Integer,Integer>();
     spMap.put(1, sp.load(this, R.raw.hit,1));
     spMap.put(2, sp.load(this, R.raw.main,1));

    }
    public void playSound(int sound,int number)
    {
     AudioManager am=(AudioManager)this.getSystemService(this.AUDIO_SERVICE);
     float audioMaxVolumn=am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
     float audioCurrentVolumn=am.getStreamVolume(AudioManager.STREAM_MUSIC);
     float volumnRatio=audioCurrentVolumn/audioMaxVolumn;
     sp.play(spMap.get(sound), volumnRatio, volumnRatio, 1, number, 1);
    }

原创粉丝点击