Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Rory Callendar
Rory Callendar
1,014 Points

Repurposed Crystal Ball Sound Issue

I re-purposed my Crystal Ball to be a random name selector to use in my classroom. My new sound effect however plays twice (overlapping)...I think because it is detecting two onShake events. Is there a way to make sure my sound plays once only by itself until the whole sound has been played to the end?

Also, anyone know where to start if I want to customize my name selector for each of my classes? I could create 5 different apps and enter names manually, but I'd rather be able to grab the names from our school's software (Pinnacle GradeBook) on one app with the click of a button.

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hi Rory,

I love this change! Thanks for sharing! :smile:

There is a MediaPlayer method called isPlaying() that you could probably use to prevent the sound from playing twice. I have some code I can paste in, but why don't you try getting it to work yourself, first? Stop back in and let us know if you get it or if you need more help. Here are some guidelines:

  1. Right now we are declaring the MediaPlayer variable in the playSound() method. Move the declaration so that player is a class member variable. That way it's the same variable being used each time playSound() is called (as opposed to having new ones created each time).
  2. With that changed, you could then use the isPlaying() method to control how the player is created and started inside the playSound() method.

Hope this helps!