Accessing Assets

BeatBox will end up doing a lot of work related to asset management: finding assets, keeping track of them, and eventually playing them as sounds. To manage all this, create a new class called BeatBox in com.bignerdranch.android.beatbox. Go ahead and add a couple of constants: one for logging and one to remember which folder you saved your boxing grunts in.

Listing 19.10  New BeatBox class (BeatBox.kt)

private const val TAG = "BeatBox"
private const val SOUNDS_FOLDER = "sample_sounds"

class BeatBox {

}

Assets are accessed using the AssetManager class. You can get an AssetManager from any Context. Since BeatBox will need one, give it a constructor that takes in an AssetManager and stashes it for later.

Listing 19.11  Stashing an AssetManager for safekeeping (BeatBox.kt)

private const val TAG = "BeatBox"
private const val SOUNDS_FOLDER = "sample_sounds"

class BeatBox(private val assets: AssetManager) {

}

When accessing assets, in general you do not need to worry about which Context you are using. In every situation you are likely to encounter in practice, every Context’s AssetManager will be wired up to the same set of assets.

To get a listing of what you have in your assets, you can use the list(String) function. Write a function called loadSounds() that looks in your assets with list(String).

Listing 19.12  Looking at assets (BeatBox.kt)

class BeatBox(private val assets: AssetManager) {

    fun loadSounds(): List<String> {
        try {
            val soundNames = assets.list(SOUNDS_FOLDER)!!
            Log.d(TAG, "Found ${soundNames.size} sounds")
            return soundNames.asList()
        } catch (e: Exception) {
            Log.e(TAG, "Could not list assets", e)
            return emptyList()
        }
    }
}

AssetManager.list(String) lists filenames contained in the folder path you pass in. By passing in your sounds folder, you should see every .wav file you put in there.

To verify that this is working correctly, create an instance of BeatBox in MainActivity and invoke the loadSounds() function.

Listing 19.13  Creating a BeatBox instance (MainActivity.kt)

class MainActivity : AppCompatActivity() {

    private lateinit var beatBox: BeatBox

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        beatBox = BeatBox(assets)
        beatBox.loadSounds()

        val binding: ActivityMainBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_main)

        binding.recyclerView.apply {
            layoutManager = GridLayoutManager(context, 3)
            adapter = SoundAdapter()
        }
    }
    ...
}

Run your app, and you should see some log output telling you how many sound files were found. We provided 22 .wav files, so you should see:

    …1823-1823/com.bignerdranch.android.beatbox D/BeatBox: Found 22 sounds
..................Content has been hidden....................

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