A (175)

(Tuis.) #1
CHAPTER 12: Digital Audio: Providing Aural Feedback for UI Designs Using SoundPool 471

Adding SoundPool to Your UI: Calling .triggerSample()


Let’s call your .triggerSample(int sound, float pitch) method, which you wrote in the previous
section of this chapter, inside of each of your three ImageButton onClick( ) event handler methods.
This will invoke all of the code that you have been writing during the chapter to play your three
sound effect samples using the SoundPool audio sequencing engine.


This is the easy part of the process, and involves writing three fairly simple lines of code, to be
placed in each of your three ImageButton onClick( ) event handler methods, right after (or before, if
you prefer) the object and method calls which set up your ImageView and TextView UI elements.


Add a triggerSample(1, 1.0f) method call to the imageButtonOne event handler,
a triggerSample(2, 1.0f) method call to the imageButtonTwo event handler, and a
triggerSample(3, 1.0f) method call to the imageButtonThree event handler methods, respectively.


As an example of what one of the three .setOnClickListener( ) event handler Java code structures
should look like, here is the Java code for imageButtonOne, as is shown in Figure 12-25:


imageButtonOne.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
animImageView.setBackgroundResource(R.drawable.imageviewwhitering);
animImageView.setImageResource(R.drawable.anim_milkyway);
animImageView.setAnimation(null);
nameData.setText("Milky Way");
solarData.setText("511");
habitData.setText("97");
colonyData.setText("37579231");
popData.setText("1967387132");
fleetData.setText("237");
shipsData.setText("34769");
triggerSample(1, 1.0f);
}});

Free download pdf