Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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) {

        sounds.remove("sounds"+Integer.toString(ncount));
    }


});

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

Thanks!

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

2 Answers 2

Use a SoundPool

share|improve this answer

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

 
discard

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.