Chapter 20 Data Binding and MVVM
Figure 20.13 All done
Accessing Assets
You have finished all the work for this chapter. You will develop BeatBox to actually use the content of
your assets in the next chapter.
Before you do, though, let’s discuss a bit more about how assets work.
Your Sound object has an asset file path defined on it. Asset file paths will not work if you try to open
them with a File; you must use them with an AssetManager:
String assetPath = sound.getAssetPath();
InputStream soundData = mAssets.open(assetPath);
This gives you a standard InputStream for the data, which you can use like any other InputStream in
Java.
Some APIs require FileDescriptors instead. (This is what you will use with SoundPool in the next
chapter.) If you need that, you can call AssetManager.openFd(String) instead:
String assetPath = sound.getAssetPath();
// AssetFileDescriptors are different from FileDescriptors,
AssetFileDescriptor assetFd = mAssets.openFd(assetPath);
// but you get can a regular FileDescriptor easily if you need to.
FileDescriptor fd = assetFd.getFileDescriptor();
For the More Curious: More About Data Binding
Complete coverage of data binding is outside the scope of this book. And yet, reader, we must
nevertheless try.