I am creating an application that will have multiple images on the screen, these images will be buttons and when tapped will play a short sound. I researched this and could
You can use SoundPool
. It fits what you want to do perfectly.
You'll just need a way to store the sound effect IDs corresponding to each image (or button).
Perhaps extend Button to store associated sound effect ID. And use a common SoundPool to play the sound effect associated to the id when the button is touched.
You can read more about SoundPool here.
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
}
mp.start();
} catch (Exception e) {
e.printStackTrace();
}
@Rikonator is on the right track there with Soundpool. It's much more suited to the kind of functionality you are after.
If you decide to go with the mediaplayer anyway, though, don't forget the prepareAsync () method to prevent it from hanging the UI thread. You can read more about playing media here.