Possible SoundPool bug in setLoop

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Possible SoundPool bug in setLoop

Postby piisam12 » Mon Jun 07, 2010 12:23 pm

I've experimented with SoundPool a bit and it seems that setLoop loops only samples with framecount under 2048. I'd like to hear any comments about this. Has anybody successfully looped a longer sample with SoundPool? If this is indeed a bug and not some kind of "feature" that I'm unaware of, this bould be submitted into the official issue tracker.

Following ERRORs will be produced if samples which have a size of over 2048 frames are set to loop:
06-05 17:32:50.279: ERROR/AudioTrack(399): setLoop invalid value: loopStart 0, loopEnd 37485, loopCount -1, framecount 2048, user 0
06-05 17:32:50.289: ERROR/AudioTrack(399): setLoop invalid value: loopStart 0, loopEnd 5856, loopCount -1, framecount 2048, user 0

Somehow it seems that framecount is capped at 2048.

SoundPool uses AudioTrack and in AudioTrack.cpp there's code like this:

Code: Select all
if (loopStart >= loopEnd ||
        loopEnd - loopStart > mFrameCount) {
        LOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, user %d", loopStart, loopEnd, loopCount, mFrameCount, cblk->user);
        return BAD_VALUE;
    }


So, if loopend minus loopstart are larger than mFrameCount, looping isn't set and BAD_VALUE is returned. If mFrameCount is capped at 2048 for some reason (AudioTrack limitation?), looping works only for very very short samples. However, many applications may require longer loops which are still classified as sound effects rather than background music. For example rain ambience loops or similar.

However, classfications aside, if SoundPool has a limitation like this and it's not a bug, it should be stated in the API reference.

To reproduce: Insert three wav samples that are of different size into res/raw with names sample1.wav, sample2.wav, and shortsample.wav. Shortsample.wav must be very small resulting in a framecount of 2048 or less. Then implement the following code. Also create a button into the ui layout. Clicking a button plays the sounds and tries to set them looping. This results in the error messages shown above and only the shortsample.wav looping.

Code: Select all
package somepack.SoundPoolTest;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SoundPoolTest extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
   
   SoundPool a;
   int soundid1;
   int soundid2;
   int soundid3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        a = new SoundPool(16, AudioManager.STREAM_MUSIC,0);
       
        soundid1 = a.load(this.getBaseContext(),  R.raw.sample1, 1);
        soundid2 = a.load(this.getBaseContext(),  R.raw.sample2, 1);
        soundid3 = a.load(this.getBaseContext(),  R.raw.shortsample, 1);

        Button button = (Button)findViewById(R.id.Button01);
        button.setOnClickListener(this);   
    }
   
    public void onClick(View v) {
      int k = v.getId();
      if(k == R.id.Button01)
      {
         long chan1 = a.play(soundid1, 1.0f, 1.0f, 1, 0, 1.0f);
           a.setLoop((int) chan1, -1);
          
           long chan2 = a.play(soundid2, 1.0f, 1.0f, 1, 0, 1.0f);
           a.setLoop((int) chan2, -1);
          
           long chan3 = a.play(soundid3, 1.0f, 1.0f, 1, 0, 1.0f);
           a.setLoop((int) chan3, -1);
      }
    }
}
piisam12
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jun 07, 2010 12:18 pm

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 1 guest