Accessing Assets

Recall that your sound files are stored in your app’s assets. Before you access those files to play the audio, 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:

    val assetPath = sound.assetPath

    val assetManager = context.assets

    val soundData = assetManager.open(assetPath)

This gives you a standard InputStream for the data, which you can use like any other InputStream in Kotlin.

Some APIs require FileDescriptors instead. This is what you will use with SoundPool. If you need that, you can call AssetManager.openFd(String) instead:

    val assetPath = sound.assetPath

    val assetManager = context.assets

    // AssetFileDescriptors are different from FileDescriptors...
    val assetFileDescriptor = assetManager.openFd(assetPath)

    // ... but you can get a regular FileDescriptor easily if you need to
    val fileDescriptor = assetFileDescriptor.fileDescriptor
..................Content has been hidden....................

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