我有一个应用程序,通过单击按钮从我的原始文件夹中播放随机声音。它工作正常,但我尝试将按钮上的文本设置为下一个文件名失败。我远吗? 另外,我如何按顺序播放文件而不是随机播放?谢谢,请温柔。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sound = (Button) findViewById(R.id.button1);
sound.setOnClickListener(new View.OnClickListener() {
final int[] btns = new int[]{R.raw.sound1, R.raw.sound2, R.raw.sound3, R.raw.sound4, R.raw.sound5, R.raw.sound6, R.raw.sound7, R.raw.sound8, R.raw.sound9, R.raw.sound10, R.raw.sound11, R.raw.sound12, R.raw.sound13, R.raw.sound14, R.raw.sound15, R.raw.sound16, R.raw.sound17, R.raw.sound18, R.raw.sound19, R.raw.sound20, R.raw.sound21, R.raw.sound22, R.raw.sound23, R.raw.sound24, R.raw.sound25, R.raw.sound26, R.raw.sound27, R.raw.sound28, R.raw.sound29, R.raw.sound30, R.raw.sound31, R.raw.sound32, R.raw.sound33, R.raw.sound34, R.raw.sound35, R.raw.sound36, R.raw.sound37, R.raw.sound38, R.raw.sound39, R.raw.sound40, R.raw.sound41, R.raw.sound42, R.raw.sound43, R.raw.sound44, R.raw.sound45, R.raw.sound46, R.raw.sound47, R.raw.sound48, R.raw.sound49, R.raw.sound50 };
private Random rnd = new Random();
@Override
public void onClick(View v) {
((Button) v).setText(rnd);
int sndToPlay = rnd.nextInt(btns.length);
final MediaPlayer mp = MediaPlayer.createMainActivity.this, btns[sndToPlay]);
mp.start();
}
);
}
答案 0 :(得分:0)
你的一些代码有点偏,但这是更新的功能。您试图传入随机对象而不是传入声音文件名。
@Override
public void onClick(View v) {
int sndToPlay = rnd.nextInt(btns.length);
((Button) v).setText(sndToPlay);
final MediaPlayer mp = MediaPlayer.createMainActivity(btns[sndToPlay]);
mp.start();
}