Importing Assets

First, you need to add the sound files to your project so your code can use them at runtime. Rather than use the resources system for this job, you will use raw assets. You can think of assets as stripped-down resources: They are packaged into your APK like resources, but without any of the configuration system tooling that goes on top of resources.

In some ways, that is good. Because there is no configuration system, you can name assets whatever you want and organize them with your own folder structure. In other ways, though, it is bad. Without a configuration system, you cannot automatically respond to changes in pixel density, language, or orientation, nor can you automatically use the assets in layout files or other resources.

Usually resources are the better deal. However, in cases where you only access files programmatically, assets can come out ahead. Most games use assets for graphics and sound, for example – and so will BeatBox.

Your first step will be to import your assets. Create an assets folder inside your project by right-clicking on your app module and selecting NewFolderAssets Folder. In the dialog that pops up, leave the Change Folder Location checkbox unchecked and leave the Target Source Set set to main (Figure 19.3).

Figure 19.3  Creating the assets folder

Creating the assets folder

Click Finish to create your assets folder.

Next, right-click on assets to create a subfolder for your sounds by selecting NewDirectory. Enter sample_sounds for the directory name (Figure 19.4).

Figure 19.4  Creating the sample_sounds folder

Creating the sample_sounds folder

Everything inside the assets folder will be deployed with your app. For the sake of convenience and organization, you created a subfolder called sample_sounds. Unlike with resources, a subfolder is not required for assets. You include it to organize your sounds.

So where can you find the sounds? You will be using a Creative Commons-licensed sound set we initially found provided by the user plagasul at freesound.org/​people/​plagasul/​packs/​3/. We have put them in a zip file for you at bignerdranch.com/​solutions/​sample_sounds.zip.

Download the zip file and unzip its contents. Copy or move the unzipped sound files into assets/sample_sounds (Figure 19.5).

Figure 19.5  Imported assets

Imported assets

(Make sure only .wav files are in there, by the way – not the .zip file you got them from.)

Build your app to make sure everything is hunky-dory. The next step will be to list those assets and show them to the user.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.209.63.120