سؤال

In my particular case, I am programming a fartbutton-app.
But I'd like to know how to do this - especially for another, more serious app.

My problem is that I have three different farting sounds in the res/raw directory.
For now, I manually loaded them individually like this:

// soundPool is the class's attribute.
// Initialize SoundPool with all sounds:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

soundCollection[0] = soundPool.load(this, R.raw.fart1, 0);
soundCollection[1] = soundPool.load(this, R.raw.fart2, 0);
soundCollection[2] = soundPool.load(this, R.raw.fart3, 0);

I'd like to be able to simply load all files in (any) particular folder.
That way I could easily add more sounds, without having to import them manually.

In the case of an fartbutton-app, this is not really an issue.
But for a sort of vocabulary app, being able to add new words into that folder and dynamically importing them is a key feature.

Many suggestions I found while searching didn't go into much detail and left me unable to follow them.
Most of the time they also were about loading files from the SD-card.

If you could explain me (as extensively as possible) a way to go about this, I would greatly appreciate your time!

هل كانت مفيدة؟

المحلول

Here we go, deleted my old answer, this should be a more direct solution:

Place subfolder in your assets directory, and the following should iterate and open each asset.

    String[] file_arr = null;
    Context m_context = getActivity();

    try {
        file_arr = m_context.getAssets().list(subfolderName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    InputStream is;

    for(String filename : file_arr){
        try {
            is = m_context.getAssets().open(filename);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //..do something

    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top