Importing Assets

Now to add the sound files to your project and read them in 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. Leave the Change Folder Location checkbox unchecked, and leave the Target Source Set set to main (Figure 20.3).

Figure 20.3  Creating the assets folder

Screenshot shows Customize the Activity screen.

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 20.4).

Figure 20.4  Creating the sample_sounds folder

Screenshot shows New Directory window. Enter new directory name field reads, sample_sounds. The Cancel and OK (selected) buttons are placed below.

Everything inside of the assets folder is 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. 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 www.freesound.org/​people/​plagasul/​packs/​3/. We have put them in a zip file for you at the following location:

Download the zip file and unzip its contents into assets/sample_sounds (Figure 20.5).

Figure 20.5  Imported assets

Screenshot shows imported assets. The Android window shows app menu with Java option selected.

(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
3.131.13.194