I am playing sounds in my application I have the following code

sounds = new HashMap<String, MediaPlayer>();

Then when guys are created on screen they are given a sound.

sounds.put("sounds"+Integer.toString(ncount), MediaPlayer.create(this, R.raw.hit));

I want to remove the MediaPlayer from the hashmap like I do the enemies when they die

sounds.get("sounds"+Integer.toString(ncount)).setOnCompletionListener( new MediaPlayer.OnCompletionListener() {

    public void onCompletion(MediaPlayer mp) {



This errors out... how would I reference the sounds hashmap to remove the associated MediaPlayer when sound is complete?


  • 3
    \$\begingroup\$ What error are you getting? \$\endgroup\$ – Firas Assaad Oct 15 '10 at 22:23
  • 4
    \$\begingroup\$ Additionally, some of your questions are purely programming questions and not particularly about game development. Since the number of Android developers on this site is still low, you might want to try asking on StackOverflow \$\endgroup\$ – Firas Assaad Oct 15 '10 at 22:26

Use a SoundPool


Is ncount a static variable? If that's the case it is very likely that ncount has changed when onCompletion gets called (because ncount was modified during playing of the sound). You should probably use another method to build that identifier...


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.