1
votes

it's my first question here. I searched all the way, tried a lot but couldn't get what I want to do. Here's the problem: I have an app that uses SoundPool to play sounds, just that. But there's a lot to load (>50) and it takes time to load one by one. take a look;

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_fragment_hue, container, false); DBSupport.setContext(getActivity()); lista = DBSupport.getListaHue();

    sp = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);

    final ArrayList<CharSequence> lista_string = new ArrayList<CharSequence>();

    for (int i = 0; i < lista.size(); i++) {

        Hue e = null;
        e = lista.get(i);
        e.setValor(sp.load(getActivity(), getResId(e.getNome_hue(), ListaHues.class), 1));
        lista_string.add(lista.get(i).getDescricao());
    }

And I want to make a thread or an Async to load it while I scroll through the ListView

Any advice?

PS: I'm a newbie, so sorry if the code is confuse.

Thanks!!

1

1 Answers

0
votes

What you need to do here is load all the sounds you would like to be able to play in your activity when it starts in onCreate(). Before you call load() on any sound, you should also set a single listener with setOnLoadCompleteListener() that figures out when all the sounds have been loaded. Only after the sounds are complete should you attempt to play them.

If you need the sounds all to be completely loaded before the user interacts with your UI, you may want to provide some kind of waiting indicator until all the sounds are done. When complete, show your UI. Then simply play a sound when needed and it should start playing instantly.