Sound Pool Example In Android Development

This example shows how you can play sound using sound pool.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it SoundPoolExample.

2.) Add a sound file to res/raw folder and name it to sound1.

3.) Write following into main.xml:

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@+id/textView1" android:layout_width="fill_parent"
android:layout_height="fill_parent">

 

 

4.) Run for output.

Steps:

1.) Create a project named SoundPoolExample and set the information as stated in the image.

Build Target: Android 4.0
Application Name: SoundPoolExample
Package Name: com. example. SoundPoolExample
Activity Name: SoundPoolExample
Min SDK Version: 8

2.) Open SoundPoolExample.java file and write following code there:

 

package com.example.soundpoolexample;

import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class SoundPoolExample extends Activity implements OnTouchListener {
private SoundPool soundPool;
private int soundID;
boolean loaded = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = findViewById(R.id.textView1);
view.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.sound1, 1);

}

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
return false;
}
}

 

 

3.) Compile and build the project.

4.) Make sure you run this app on actual device to get the sound played.

Output

Leave a Comment:

6 comments
Cyrille says February 20, 2013

Hello!
Thanks for your great tutorial!
What would there be to modify to play the sound(s) hitting an OTG USB keyboard key??

Thanks again!

Reply
Ashish says March 20, 2013

I am run this program …then create force close error ….
so what i do…..?????

Reply
Mike Pliam says March 20, 2013

Compiles and mounts to emulator but ‘Unfortunately, SoundPoolExample has stopped’. Also, the given class SoundPoolExample is not the default class MainActivity so I had to change the name – does this ruin the app?

When one encounters the runtime error ‘Unfortunately, …’, how does one go about debugging the code ?

Appreciate any help

Reply
Faisal says April 17, 2013

How if i want to play 2 or more sound in inmplement ontouch?

Reply
kB says April 17, 2013

Does not work !
No sound.

Reply
ronan says July 2, 2013

Hi, thanks for the great tutorial… iv applied your code but after pressing the button so many times(Android game)the sound stops working and the Log is showing me this error…E/AudioPolicyManagerBase: getOutput() IOProfile is null…..any ideas????

Kind regards

Ronan

Reply
Add Your Reply