soundPool 不在 onAnimationStart 内循环播放

soundPool don't play inside loop at onAnimationStart

我有一个 ImageView 数组,ImageView 将相互缩放。 现在我想在动画的onAnimationStart循环播放短促的叮当声(时长不到一分钟)。

一切正常但是声音只播放第一个。

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);

        }
    }
}

下面是我如何定义 soundPool 和加载声音。

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);

我该怎么做???或者有什么问题???

PS1:

好的,我将 soundPool.play() 添加到 onAnimationEnd,它工作正常。所以 soundPool 没有任何问题。我想是动画方面的

即使您设置了 AnimationstartOffsetonAnimationStart 也会在 images_cen[ck].startAnimation(scale); 之后不久调用。

我建议您改用 Animator

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();