Getting At 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 wrestling grunts in.

Listing 20.12  New BeatBox class (BeatBox.java)

public class BeatBox {
    private static final String TAG = "BeatBox";

    private static final String SOUNDS_FOLDER = "sample_sounds";
}

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 a Context as a dependency, pulls out an AssetManager, and stashes it away.

Listing 20.13  Stashing an AssetManager for safekeeping (BeatBox.java)

public class BeatBox {
    private static final String TAG = "BeatBox";

    private static final String SOUNDS_FOLDER = "sample_sounds";

    private AssetManager mAssets;

    public BeatBox(Context context) {
        mAssets = context.getAssets();
    }
}

When getting at 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) method. Write a method called loadSounds() that looks in your assets with list(String).

Listing 20.14  Looking at assets (BeatBox.java)

public BeatBox(Context context) {
    mAssets = context.getAssets();
    loadSounds();
}

private void loadSounds() {
    String[] soundNames;
    try {
        soundNames = mAssets.list(SOUNDS_FOLDER);
        Log.i(TAG, "Found " + soundNames.length + " sounds");
    } catch (IOException ioe) {
        Log.e(TAG, "Could not list assets", ioe);
        return;
    }
}

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 BeatBoxFragment.

Listing 20.15  Creating BeatBox instance (BeatBoxFragment.java)

public class BeatBoxFragment extends Fragment {

    private BeatBox mBeatBox;

    public static BeatBoxFragment newInstance() {
        return new BeatBoxFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBeatBox = new BeatBox(getActivity());
    }
    ...
}

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

…1823-1823/com.bignerdranch.android.beatbox I/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
3.138.105.124