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 could 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]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;
}[/code]
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]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);
}
}
}[/code]
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en