在Android中如何在AnimationSet中添加声音效果

时间:2015-01-29 19:42:55

标签: android soundpool objectanimator

我创建了一个AnimationSet,并希望在第一个动画后产生声音效果。是否可以以某种方式将其添加到动画集​​中,或者是否存在可以改变声音的ObjectAnimator属性。请让我知道如何做到这一点。

MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.combo);
ObjectAnimator fade = ObjectAnimator.ofFloat(ImageButton,"alpha",0f,1f);
ObjectAnimator move = ObjectAnimator.ofFloat(myAnimation1,"translationX",20);
ObjectAnimator moveX = ObjectAnimator.ofFloat(myAnimation1,"translationY",20);
AnimatorSet as =new AnimatorSet();
                as.playTogether(move,moveY,fade);//wanted to know if I could add the sound  mp after moveY
                as.start();

1 个答案:

答案 0 :(得分:0)

一种可能性是向ObjectAnimator添加一个监听器。例如:

 mAnimator.addListener(new AnimatorListenerAdapter(){
      @Override public void onAnimationStart(Animator animator){
        // do something if needed when animation starts, play a sound maybe
      }
      @Override public void onAnimationEnd(Animator animator){
        // call your sound player from here
      }
    }
);

将在每个阶段触发方法。您需要将MediaPlayer变量设为 final 变量,除非它具有更高的范围。

如果你的音频片段很短,那么当我们想要发射多个片段时,SoundPool可能更适合这项工作。但在决定之前,您可能需要查看以下内容:AudioTrack, SoundPool or MediaPlayer Which Should I use?