Android 即时音效

 

 SoundPool sp;
   HashMap spMap;

 public void initSoundPool(){
     sp=new SoundPool(5,AudioManager.STREAM_MUSIC,0);
     spMap=new HashMap();
     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);
    }

你可能感兴趣的:(Android)