【发布时间】:2016-04-04 02:46:47
【问题描述】:
How to make Random sound when button click?
我对编程世界很陌生,我之前检查过上面的链接,尝试将其用作播放随机声音的参考。我正在使用 SoundPool,因为我知道它在播放短片方面比 MediaPlayer 更好。我总共有四种声音。
当我运行我的应用程序时,我收到一条错误消息,说不幸的是它已停止。
关于什么可能是错误的任何想法?
这是我的代码:
import java.util.Random;
public class actibida extends AppCompatActivity {
SoundPool soundPool;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actibida);
final int[] sound = new int[4];
sound[0] = soundPool.load(actibida.this, R.raw.el, 1);
sound[1] = soundPool.load(actibida.this, R.raw.guau, 1);
sound[2] = soundPool.load(actibida.this, R.raw.miau, 1);
sound[3] = soundPool.load(actibida.this, R.raw.quack, 1);
final Random r = new Random();
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View button) {
soundPool.play(sound[r.nextInt(4)], 1.0f, 1.0f, 0, 0, 1.0f);
}
});
}
}
【问题讨论】:
标签: java android audio playback soundpool