【发布时间】:2017-01-05 12:51:37
【问题描述】:
请帮帮我,我想通过一个按钮使用 yate Fisher shuffle 算法随机播放原始文件夹 android studio 的声音。 我使用此代码:
public void playSound() {
MediaPlayer mp;
int[] rawSoal = {R.raw.al, R.raw.fear, R.raw.love};
shuffleArray(rawSoal);
for (int i = 0; i < rawSoal.length; i++) ;
try {
Random random = new Random();
mp = MediaPlayer.create(this, rawSoal[random.nextInt(rawSoal.length)]);
mp.start();
mp.release();
}catch(Exception e){
Log.e("ERROR", "Media Player", e);
mp = null;
}
}
实现Fisher-Yates shuffle
static void shuffleArray(int[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
通话按钮:
plybtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
playSound();
mp.release();
}
});
【问题讨论】:
-
使用上述代码遇到的问题是什么
-
运行时,不幸的是,已经停止
-
你为什么用这个:for (int i = 0; i