5

I have a bunch of sounds, assigned to a group of buttons, which i need to play. All my sounds are in asset folder. However, it does not work. The purpose is: to load from assetFodler and play that sounds. I will come out with code examples from my project:

//set up  audio player
    mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
    mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

 //getting files lists from asset folder
    aMan = this.getAssets();
    try {
        filelist = aMan.list("");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

For the purpose of do not having a lot of code lines, I created a basic procedure for loading and playing sounds:

public void loadSound (String strSound, int stream) {

    try {
        stream= mSoundPool.load(aMan.openFd(strSound), 1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
}

As you can see I pass file(stringName) and streamID.

Finally, here is how i use it:

     case R.id.button1:
        //if button was clicked two or more times, when play is still on im doing stop
    mSoundPool.stop(mStream1);
    loadSound(filelist[0],mStream1);
        break;

When I run project, nothing happens and logcat says:

12-09 10:38:34.851: W/SoundPool(17331):   sample 2 not READY

Any help would be appreciated.

UPD1: When I do it this way, without having loadSound procedure it works fine the following code is onCreate:

//load fx
    try {
        mSoundPoolMap.put(RAW_1_1, mSoundPool.load(aMan.openFd(filelist[0]), 1));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

and Onclick button:

  //resourcePlayer.stop();
        mSoundPool.stop(mStream1);
        mStream1= mSoundPool.play(mSoundPoolMap.get(RAW_1_1), streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);

I just dont want to have so much code lines, i wanted to make it look nice

Daler
  • 1,205
  • 3
  • 18
  • 39

1 Answers1

3

You will need to check file is loaded successfully before playing it using SoundPool.setOnLoadCompleteListener

Change your loadSound method code as:

public void loadSound (String strSound, int stream) {
     boolean loaded = false;
     mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,
                    int status) {
                loaded = true;
            }
        });
    try {
          stream= mSoundPool.load(aMan.openFd(strSound), 1);
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   // Is the sound loaded already?
   if (loaded) {
     mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
    }
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • well, nothing happens. seems it is not loading... why? – Daler Dec 09 '12 at 06:17
  • @Daler : because everything executing in a sequence in your current code so make some wait after `mSoundPool.load(aMan.openFd(strSound), 1); `called .you can also see http://www.vogella.com/blog/2011/06/27/android-soundpool-how-to-check-if-sound-file-is-loaded/ example – ρяσѕρєя K Dec 09 '12 at 06:21
  • unfortunately, i must not have any delays when user will click on button. It has to play immediately. Any ideas of how to achieve it? Actually I can make it to play immediately by using R.raw, but i want to do it from assets.. – Daler Dec 09 '12 at 06:26
  • then try after moving `mSoundPool.load(aMan.openFd(strSound), 1); ` inside activity onCreate method and `if (loaded) { mSoundPool.play(stream, streamVolume, streamVolume, 1, LOOP_1_TIME, 1f); }` lines inside onResume of Activity – ρяσѕρєя K Dec 09 '12 at 06:29
  • @Daler : or u can also implement a Thread for playing music when mSoundPool.load is ready. – ρяσѕρєя K Dec 09 '12 at 06:31
  • 1
    i have about 20+ sound which has to be played, and they are all in one activity, so dont see a point to have another thread. Having them load from asset folder would make my job much easer, when i will create a list of that sounds... thanks ) – Daler Dec 09 '12 at 06:33
  • I suggest you also check what's in your filelist variable – CocoNess Dec 09 '12 at 06:35
  • @TanjaV, and ρяσѕρєя K i have updated my code.. any ideas now? – Daler Dec 09 '12 at 06:41