This only loads one sound but you can load as many as you want.
Put an mp3 in the res/raw directory called Explosion.mp3
Using java Syntax Highlighting
- public static final int SOUND_EXPLOSION = 1;
- public static final int SOUND_YOU_WIN = 2;
- public static final int SOUND_YOU_LOSE = 3;
- private SoundPool soundPool;
- private HashMap<Integer, Integer> soundPoolMap;
- private void initSounds() {
- soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
- soundPoolMap = new HashMap<Integer, Integer>();
- soundPoolMap.put(SOUND_EXPLOSION, soundPool.load(getContext(), R.raw.explosion, 1));
- }
- public void playSound(int sound) {
- AudioManager mgr = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
- int streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
- soundPool.play(soundPoolMap.get(sound), streamVolume, streamVolume, 1, 0, 1f);
- }
- public void update() {
- if (isExploding()) {
- playSound(SOUND_EXPLOSION);
- }
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
NOTE: There are some issues with this object. Read this thread for more info.
SDK Version compatible: 0.9, 1.0
Thanks to rbgrn for creating and sharing this snippet!



.


