Wiring Up Assets for Use

Now that you have your asset filenames, you should present them to the user. Eventually, you will want the files to be played, so it makes sense to have an object responsible for keeping track of the filename, the name the user should see, and any other information related to that sound.

Create a Sound class to hold all of this.

Listing 19.14  Creating Sound object (Sound.kt)

private const val WAV = ".wav"

class Sound(val assetPath: String) {

    val name = assetPath.split("/").last().removeSuffix(WAV)
}

In the constructor, you do a little work to make a presentable name for your sound. First, you split off the filename using String.split(String).last(). Once you have done that, you use String.removeSuffix(String) ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.