package com.bignerdranch.android.beatbox;

import android.content.Context;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BeatBox {
    private static final String TAG = "BeatBox";
    private static final String SOUNDS_FOLDER = "sample_sounds";
    private static final int MAX_SOUNDS = 5;

    private AssetManager mAssetsManager;
    private List<Sound> mSounds = new ArrayList<>();
    private SoundPool mSoundPool;

    public BeatBox(Context context) {
        mSoundPool = new SoundPool(MAX_SOUNDS, AudioManager.STREAM_MUSIC, 0);
        mAssetsManager = context.getAssets();
        loadSounds();
    }

    private void loadSounds() {
        String[] soundNames;
        try {
            soundNames = mAssetsManager.list(SOUNDS_FOLDER);
            Log.i(TAG, "Found " + soundNames.length + " sounds");
        }
        catch (IOException ioe) {
            Log.e(TAG, "Couldn't list assets", ioe);
            return;
        }

        for (String soundName : soundNames) {
            String assetPath = SOUNDS_FOLDER + "/" + soundName;
            Sound sound = new Sound(assetPath);
            mSounds.add(sound);
        }
    }

    public List<Sound> getSounds() {
        return mSounds;
    }
}

@@ -68,16 +68,64 @@
 ontext;%0A
+import android.content.res.AssetFileDescriptor;%0A
 import a
@@ -1205,24 +1205,46 @@
 undNames) %7B%0A
+            try %7B%0A    
             
@@ -1295,16 +1295,20 @@
 ndName;%0A
+    
         
@@ -1363,37 +1363,415 @@
     
-mSounds.add(sound);%0A        %7D
+    loadSound(sound);%0A                mSounds.add(sound);%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void loadSound(Sound sound) throws IOException %7B%0A        AssetFileDescriptor fileDescriptor = mAssetsManager.openFd(sound.getAssetPath());%0A        int soundId = mSoundPool.load(fileDescriptor, 1);%0A        sound.setSoundId(soundId);
 %0A   

Load sounds to SoundPool