Problem solved. Here's how I did it:
This only loads one sound but you can load as many as you want. I have
an mp3 in my res/raw directory called explosion.mp3.
Java:
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);
}
}
On Oct 11, 12:11 pm, Robert Green <[EMAIL PROTECTED]> wrote:
> I'm adding sound into my game now and need the ability to have sound
> effects concurrent (like 2 of the same explosion playing at the same
> time, etc) so using the MediaPlayer is less than optimal for me. I was
> thinking of just adding a traditional sound update into the main loop
> which would probably work and isn't _that_ hard to write but I don't
> think I have access to the decoder and sound buffer.
>
> I found a class called SoundPool that appears to be suited for exactly
> what I'm trying to do however the documentation is very weak
> -http://code.google.com/android/reference/android/media/SoundPool.html
>
> Has anyone used this with success? If so, could I see a little code
> snippet?
>
> I tried this:
> Java:
>
> private void initSounds() {
> // 4 streams
> // stream type 3 is music
> soundPool = new SoundPool(4, 3, 100);
> int explosionSound = soundPool.load(getContext(),
> R.raw.explosion, 1);
> soundPool.play(explosionSound, 100, 100, 1, 0, 1);
> }
>
> but it didn't appear to do anything. I didn't get errors but I also
> heard no sound. Perhaps I need to configure something for the emulator
> to work like that?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---