I was working on a simple bubblewrap app in order to learn the android framework, and was wondering how I could play the 'pop' sound clip every time the user touches a bubble.
Thanks in advance
Use a SoundPool
combined with an OnTouchListener
.
In your onTouch(..)
method you would simply do a SoundPool.play(YOURAUDIOFILE, 1, 1, 1, 0, 1);
Gl hf
I know it's a cop out to just link to someone else's explanation, but I had to do the exact same thing, and here's the tutorial I used: http://www.droidnova.com/creating-sound-effects-in-android-part-2,695.html
You Can use MedaiPlayer as following inside onTouchListener:
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_RING);
try {
mPlayer.setDataSource(mContext,
Uri.parse("android.resource://" + mContext.getPackageName() + "/" + R.raw.YourFileName));
mPlayer.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "Could not setup media player for ringtone");
mPlayer = null;
return;
}
mPlayer.setLooping(false);
mPlayer.start();