From the course: Android Development Tips

Unlock this course with a free trial

Join today to access over 22,400 courses taught by industry experts.

Play sounds with SoundPool

Play sounds with SoundPool - Android Tutorial

From the course: Android Development Tips

Play sounds with SoundPool

- [Instructor] In a previous tip, I described how to play a long running sound that's packaged as an app asset with media player. I showed how to copy the file from the assets directory to internal storage, and then play the file from there. With shorter sounds, where the file is less than one megabyte in size, you can play the file with SoundPool. The SoundPool class can load a file directly from assets, so you don't have to take the step of copying it to a new location. And you can cache the file in memory, as an activity starts up, so you won't have any latency the first time you play it. This makes SoundPool ideal for game sounds, and other effects that have to be played multiple times. In this starting project, in the assets directory, I've included two wave files. They're short files, and they're each less than one megabyte in size, so they're ideal for demonstrating the SoundPool class. In the main activity class, I'll declare a couple of private fields. The first will be an…

Contents