Wednesday, April 17, 2013

[android help] my soundpool project won't play properly?


I've been trying to implement this soundpool class (with some help from a tutorial) but when I try to run it on a virtual device, the sound won't play... The main_activity XML file loads fine, but it was expected that on loading, a sound would be played.


here is the source code.



public class MainActivity extends Activity {

private SoundPool soundpool;
private boolean soundPoolLoaded = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Create a new SoundPool instance.
// 2 = number of sounds that can be simultaneously played
// AudioManager.STREAM_MUSIC = The type of sound stream. STREAM_MUSIC is the most common one
// 0 = sound quality, default is 0.
// setOnLoadCompleteListener loads the file
soundpool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
soundpool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
soundPoolLoaded = true;
}
});

playSound("car_phonic");
}


// This method will load a sound resource from the res/raw folder by the input name given as a string (soundName).
// If the sound resource is found by name, it will attempt to play the sound
// soundPoolLoaded value becomes true when the file is fully loaded
public void playSound(String soundName) {
if(soundPoolLoaded == true)
{
// Get the current context resources and find the correct sound resource for playing the sound
Resources res = this.getResources();
int soundId = 0;
soundId = res.getIdentifier(soundName, "raw", this.getPackageName());

if(soundId != 0){
soundpool.play(soundId, 1, 1, 0, 0, 1);
}


SystemClock.sleep(500);
playSound(soundName);

}
}
}


Any help would be appreciated! Thanks



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...