Chapter 21 Unit Testing and Audio Playback
Unloading Sounds
The app works, but to be a good citizen you should clean up your SoundPool by calling
SoundPool.release() when you are done with it.
Listing 21.16 Releasing your SoundPool (BeatBox.java)
public class BeatBox {
...
public void play(Sound sound) {
...
}
public void release() {
mSoundPool.release();
}
...
}
Then, add a matching BeatBox.release() method in BeatBoxFragment.
Listing 21.17 Releasing your BeatBox (BeatBoxFragment.java)
public class BeatBoxFragment extends Fragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
}
@Override
public void onDestroy() {
super.onDestroy();
mBeatBox.release();
}
...
}
Run your app again to make sure it works correctly with your new release() method. If you play a
long sound and rotate the screen or hit the Back button, you should now hear the sound stop.