Question

Hi ive been building an app for my autistic sister and got stuck trying to create a push button play sound activity, i have read many tutorials and for one reason or another could not wrap my head around it, I've learned the differences between MediaPlayer and soundPool and think i would rather use the latter my files are in res/raw and are .ogg format (have read .wav is required but haven't got far enough to test it.) so if for example i have a cat and three buttons make up the name cat and when you press one of them it says the letter out loud ive created the layout in xml and have tried and failed a few times think mainly with the setOnClickListeners, as a result i am pretty much back at square one i was reading from this tutorial http://www.techrepublic.com/blog/app-builder/getting-your-feet-wet-in-androids-soundpool/877 but its broken up strangely with everywhere making it hard for a beginner to interpret? can anybody help?

Was it helpful?

Solution

Try this

Add this before your onCreate

SoundPool sp;
int yourSound = 0;

And this inside your onCreate

 sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
 yourSound = sp.load(this, R.raw.whatEverTheNameOfYourSound, 1);

Then add this in your button on your XML

android:onClick="playSound"

And last add this method on your activity

 public void playSound(View v){
    if(yourSound != 0)
        sp.play(yourSound, 1, 1, 0,0, 1);
}

Don't forget to add the sound file on your raw folder. Hope it helps.

the sp.play(yourSound, 1, 1, 0,0, 1); means sp.play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate); You can see it here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top