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();
..................Content has been hidden....................

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