Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 20  Data Binding and MVVM


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


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. (Remember to let Android Studio generate your getters.)


Listing 20.16  Creating Sound object (Sound.java)


public class Sound {
private String mAssetPath;
private String mName;


public Sound(String assetPath) {
mAssetPath = assetPath;
String[] components = assetPath.split("/");
String filename = components[components.length - 1];
mName = filename.replace(".wav", "");
}


public String getAssetPath() {
return mAssetPath;
}


public String getName() {
return mName;
}
}

Free download pdf