对于我的Android应用程序,我会显示一堆按钮,每个按钮都会在短暂的'缩放'动画后创建一个'pop'声音。可能一次最多有20个按钮,它们可以在1秒内随机缩放以便随机查看。
这可以很好地开始,但过了一会儿声音不再播放了,事实上,SoundPool中的其他一些声音也不再播放......
我的活动:
protected SoundPool soundPool;
protected void onCreate(Bundle savedInstanceState) {
...
this.soundPool = new SoundPool(100, AudioManager.STREAM_MUSIC, 0);
this.popAId = this.soundPool.load(this.getApplicationContext(), R.raw.pop_a, 1);
...
FlowLayout answersLayout = (FlowLayout) findViewById(R.id.answerbuttons_layout);
for (int i = 0; i < answers.size(); i++) {
addAnswerButton(savedInstanceState, answersLayout, answers.get(i));
}
...
}
protected Button addAnswerButton(Bundle savedInstanceState, FlowLayout answersLayout, String text) {
Button b = new Button(this);
b.setText(text);
answersLayout.addView(b);
class myRunnable implements Runnable {
protected Button b = null;
public myRunnable(Button b) {
this.b = b;
}
@Override
public void run() {
scaleView(this.b, 0, 1);
}
}
new android.os.Handler().postDelayed(new myRunnable(b), (long) (Math.random() * 1000));
}
return b;
}
public void scaleView(View v, float startScale, float endScale) {
Animation anim = new ScaleAnimation(
startScale, endScale, // Start and end values for the X axis scaling
startScale, endScale, // Start and end values for the Y axis scaling
Animation.RELATIVE_TO_SELF, 0.5f, // Pivot point of X scaling
Animation.RELATIVE_TO_SELF, 0.5f); // Pivot point of Y scaling
anim.setDuration(250);
anim.setFillAfter(true); // Needed to keep the result of the animation
class myAnimationListener implements Animation.AnimationListener {
protected View v = null;
public myAnimationListener(View v) { this.v = v; }
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// Play pop sound
Log.i("Hello", "World");
Log.i("GameActivity::scaleView", String.format("%1$d", soundPool.play(popAId, 1, 1, 1, 0, 1)));
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
anim.setAnimationListener(new myAnimationListener(v));
v.startAnimation(anim);
}