Search code examples
androidanimationsoundpool

soundPool don't play inside loop at onAnimationStart


I have an array of ImageView that ImageViews will scale after each other. Now i want to play short ding sound ( duration is less than a minute ) at onAnimationStart of animation at a loop.

Everything work right but the sound play only for first one.

public void AnimationAtEnd() {

    for (ck = 0; ck < totalCount; ck++) {

            ScaleAnimation scale = new ScaleAnimation(0f, 1.6f,0f,1.6f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

            scale.setInterpolator(new LinearInterpolator());
            scale.setStartOffset(ck * 600);
            scale.setDuration(600);
            scale.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {    
                       mySoundPool.play(2, 1, 1, 1, 0, 1);          
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

            images_cen[ck].startAnimation(scale);

        }
    }
}

and here is how i define soundPool and load sounds.

mySoundPool = new SoundPool(
                3,
                AudioManager.STREAM_MUSIC,
                0
        );
    mySoundPool.load(this, R.raw.circuit, 1);
    mySoundPool.load(this, R.raw.ding, 1);
    mySoundPool.load(this, R.raw.block, 1);

How can i do that ??? or What's the problem ???

PS1:

OK i add soundPool.play() to onAnimationEnd and it work fine . So there is nothing wrong with soundPool . I think It's from Animation side


Solution

  • The onAnimationStart will be called shortly after images_cen[ck].startAnimation(scale); even though you set the startOffset of the Animation.

    I suggest you use Animator instead.

    ObjectAnimator scale = ObjectAnimator.ofPropertyValuesHolder(images_cen[ck],
                        PropertyValuesHolder.ofFloat(View.SCALE_X, 0, 1.6f),
                        PropertyValuesHolder.ofFloat(View.SCALE_Y, 0, 1.6f)
                        );
    scale.setInterpolator(new LinearInterpolator());
    scale.setStartDelay(ck * 600);
    scale.setDuration(600);
    final View view = images_cen[ck];
    scale.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        view.setVisibility(View.VISIBLE);
                        mySoundPool.play(2, 1, 1, 1, 0, 1);
                    }
    
                    @Override
                    public void onAnimationEnd(Animator animation) {
    
                    }
    
                    @Override
                    public void onAnimationCancel(Animator animation) {
    
                    }
    
                    @Override
                    public void onAnimationRepeat(Animator animation) {
    
                    }
                });
    scale.start();