0

I've been trying to find a good way to load a large amount of audio files. The code below works fine, but I need to load even more files and that's when I run into issues.

It's a quiz and when the users choose the correct answer, a new sound should be played. I have to load 50+ sounds.

The current way I load the files:

    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    spool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);

                Context myActivity = getApplicationContext();

                    primSounds.add(spool.load(myActivity, R.raw.int_prim_c_mel, 1));
                    primSounds.add(spool.load(myActivity, R.raw.int_prim_d_mel, 1));
                    primSounds.add(spool.load(myActivity, R.raw.int_prim_e_mel, 1));

                    kvintSounds.add(spool.load(myActivity, R.raw.int_kvint_c_mel, 1));
                    kvintSounds.add(spool.load(myActivity, R.raw.int_kvint_d_mel, 1));
                    kvintSounds.add(spool.load(myActivity, R.raw.int_kvint_e_mel, 1));

                    oktSounds.add(spool.load(myActivity, R.raw.int_okt_c_mel, 1));
                    oktSounds.add(spool.load(myActivity, R.raw.int_okt_d_mel, 1));
                    oktSounds.add(spool.load(myActivity, R.raw.int_okt_e_mel, 1));

                    ...

The problem is that this way make the onCreate "too heavy"

johgru
  • 241
  • 2
  • 5
  • 20
  • Do you need all 50 loaded at the same time? Couldn't you load some then reload another set after they are used? – codeMagic Mar 20 '15 at 17:43
  • Maybe, the quiz questions are random so the loading has to be fast – johgru Mar 20 '15 at 17:45
  • Can you know which audios are you going to need for every quiz before you show it? In such case you could load the audios for each quiz. So, Before showing quiz a: load all "a" audios When quiz "a" is showing: load quiz b audios And so on – mmark Mar 20 '15 at 17:56
  • The quiz uses every audio on the highest difficulty – johgru Mar 20 '15 at 17:57

1 Answers1

0

Here you have a possible workaround.

But in your case, you'll have 50 sounds loaded at once at some point. This means that you'll have quite a LOT of memory in use. Having to face a similar situation before myself ,i think you can use the following code:

SoundtrackManager.java

   public void prepareSoundtrack(int soundtrackId, Context context) {
        try {

            if (mMediaPlayer != null) {
                mMediaPlayer.release();
                mMediaPlayer = null;
            }


            mMediaPlayer = MediaPlayer.create(context, soundtrackId);

        } catch (Exception e) {
            mMediaPlayer.release();
            e.printStackTrace();
        }

  }

    public void play() {
        if (mMediaPlayer != null) {
            try {
                mMediaPlayer.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

using:

mSoundTrackManager.prepareSoundtrack(R.raw.the_sound, mContext);
mSoundTrackManager.play();

You can call play() as long as the sound being played does not change.

The downside is having to create a MediaPlayer for each sound. But you wont have all of them on memory at the same time. Also, they are loaded/played instantly.

EDIT:

This could be improved by making a "pool" of MediaPlayer objects in the SoundtrackManager class. So, if one of the random sounds is needed and happens to be on that "pool", you can avoid the constant MediaPlayer re-setting.

Community
  • 1
  • 1
mmark
  • 1,204
  • 12
  • 19
  • Thanks! I think SoundPool is a better option because the audio files are in midi and 1-3 seconds long. – johgru Mar 21 '15 at 10:20