19 Audio Playback with SoundPool

Now that you are ready to go with your assets, it is time to actually play all these .wav files. Android’s audio APIs are fairly low level for the most part, but there is a tool practically tailor-made for the app you are writing: SoundPool.

SoundPool can load a large set of sounds into memory and control the maximum number of sounds that are playing back at any one time. So if your app’s user gets a bit too excited and mashes all the buttons at the same time, it will not break your app or overtax your phone.

Ready? Time to get started.

Creating a SoundPool

The first step is to create a SoundPool object.

Listing 19.1  Creating a SoundPool (BeatBox.java)

p​u​b​l​i​c​ ​c​l​a​s​s​ ...

Get Android Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.