Yeah there's something you need to know about SoundPool:  Init well
before playing - that is, you need to know way ahead of time what
sounds you will be using because it doesn't work well to play
immediately after initializing.  I think they attempt to initialize
asynchronously or something because I had problems when I tried to
load and play back to back like you're trying there.

When I use SoundPool, I load everything upon creation of the instance
of the game then trigger the plays whenever I need them.  Here's my
current code for SoundPoolSoundManager:

public class SoundPoolSoundManager implements SoundManager {
        private static final String TAG = "SoundPoolSoundManager";

        public static final int SOUND_1 = 1;

        private boolean enabled = true;
        private Context context;
        private SoundPool soundPool;
        private HashMap<Integer, Integer> soundPoolMap;

        public SoundPoolSoundManager(Context context) {
                this.context = context;
        }

        public void reInit() {
                init();
        }

        public void init() {
                if (enabled) {
                        Log.d(TAG, "Initializing new SoundPool");
                        //re-init sound pool to work around bugs
                        release();
                        soundPool = new SoundPool(SOUNDPOOL_STREAMS,
AudioManager.STREAM_MUSIC, 100);
                        soundPoolMap = new HashMap<Integer, Integer>();
                        soundPoolMap.put(SOUND_1, soundPool.load(context, 
R.raw.sound1,
1));
                        Log.d(TAG, "SoundPool initialized");
                }
        }

        public void release() {
                if (soundPool != null) {
                        Log.d(TAG, "Closing SoundPool");
                        soundPool.release();
                        soundPool = null;
                        Log.d(TAG, "SoundPool closed");
                        return;
                }
        }

        public void playSound(int sound) {
                if (soundPool != null) {
                        Log.d(TAG, "Playing Sound " + sound);
                        AudioManager mgr = (AudioManager)
context.getSystemService(Context.AUDIO_SERVICE);
                        int streamVolume = 
mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
                        Integer soundId = soundPoolMap.get(sound);
                        if (soundId != null) {
                                soundPool.play(soundPoolMap.get(sound), 
streamVolume,
streamVolume, 1, 0, 1f);
                        }
                }
        }

        public void setEnabled(boolean enabled) {
                this.enabled = enabled;
        }
}

On Nov 9, 4:28 am, g1bb <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I posted this on the AndDev forums as well, and I'm somewhat of a
> novice to Java.
>
> I've tried the following code after seeing the snippet 
> athttp://www.anddev.org/using_soundpool_instead_of_mediaplayer-t3115.html,
> and I can't seem to get this to work. Here's what I have:
>
> public class OneShotAlarm extends BroadcastReceiver
> {
>      private SoundPool soundPool;
>       private HashMap<Integer, Integer> soundPoolMap;
>
>     @Override
>     public void onReceive(Context context, Intent intent)
>     {
>       soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
>          soundPoolMap = new HashMap<Integer, Integer>();
>          soundPoolMap.put(1, soundPool.load(context, R.raw.sound1,
> 1));
>
>         AudioManager mgr = (AudioManager)
> context.getSystemService(Context.AUDIO_SERVICE);
>         int streamVolume =
> mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
>         soundPool.play(soundPoolMap.get(1), streamVolume,
> streamVolume, 1, 0, 1f);
>     }
>
> }
>
> You'll notice I replaced instaces of 'getContext()' with 'Context', as
> 'getContext()' doesn't seem to be available from where I have this
> code.
>
> Here is my LogCat that's being returned, and no sound is being played:
> 11-08 19:25:23.679: DEBUG/dalvikvm(1822): Trying to load lib /system/
> lib/libsoundpool.so 0x0
> 11-08 19:25:23.759: DEBUG/dalvikvm(1822): Added shared lib /system/lib/
> libsoundpool.so 0x0
> 11-08 19:25:23.819: WARN/SoundPool(1822): sample 1 not READY
>
> Any ideas on this? I've been pulling my hair out all day messing with
> MediaPlayer, then I came across SoundPool and it looked like it would
> be a great alternative.
>
> Thanks in advance.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to