Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question
What error are you getting? – Firas Assaad Oct 15 '10 at 22:23
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 – Firas Assaad Oct 15 '10 at 22:26

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...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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