I have a Soundpool that plays a short sound effect once a view animation starts. The issue is that it only plays that sound the first time and not any consecutive times.
I've check the return result of the sound.play function and its always the streamID and never zero.it is creating the streams when needed but the sound is only playing the first time. The sound is only loaded once, so it should not be a loading issue since it play the first time. I will be pasting the code below:
I'm calling createSoundPool() to build the soundpool
private void createSoundPool() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
createNewSoundPool();
} else {
createOldSoundPool();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void createNewSoundPool(){
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
sounds = new SoundPool.Builder().setMaxStreams(5)
.setAudioAttributes(attributes)
.build();
}
@SuppressWarnings("deprecation")
private void createOldSoundPool(){
sounds = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
}
This is how im using it
public MyTouchListener(final Context context, final RecyclerView recyclerView,
OnTouchActionListener onTouchActionListener){
mOnTouchActionListener = onTouchActionListener;
initAnim();
createSoundPool();
drawCardSound = sounds.load(context,R.raw.drawcard,1); //this is where the soundID is created. Only once the class is instantiated
mGestureDetector = new GestureDetectorCompat(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
return false;
}
// Find the item view that was swiped based on the coordinates
final View child = recyclerView.findChildViewUnder(e1.getX(), e1.getY());
final int childPosition = recyclerView.getChildAdapterPosition(child);
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if (mOnTouchActionListener != null && child != null) {
if(!animation.hasStarted() || animation.hasEnded()) {
child.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
int res=sounds.play(drawCardSound,3,3,1,0,1f);// sound is being played here
Log.e("test", "onAnimationStart: "+res );
}
The LogCat shows an incrementing streamID and never Zero so i think it should be firing correctly.
UPDATE: This issue is only happening on Lollipop and above I've tested it on emulators for below Lollipop and the sound is working fine. Is there some bug on the for Lollipop and above? I've tested it on a Samsung S7