不知道你能不能帮上忙。当用户单击按钮时,我试图播放随机声音(来自一组 7 种可用声音)。到目前为止,我有:
Random rand = new Random();
int rndm = rand.nextInt(6) + 1; // I have 7 random sounds to play sequentially named 'my sound' + n.
String sndName = "mysound" + rndm; // Assign a random # to the end of the sound file.
mp1 = MediaPlayer.create(getApplicationContext(), R.raw.sndName); // ERROR HERE: Expects an Int
mp1.start();
我希望我的一个声音文件(mysound1、mysound2、mysound3...)会随机播放,但 eclipse 提示 mp1 分配需要一个 int。有什么想法吗?
非常感谢。
最佳答案
使用
int[] sounds={R.raw.mysound1, R.raw.mysound2, R.raw.mysound3,R.raw.mysound4,R.raw.mysound5,R.raw.mysound6,R.raw.mysound7};
然后
Random r = new Random();
int Low = 0;
int High = 7;
int rndm = r.nextInt(High-Low) + Low;
mp1 = MediaPlayer.create(getApplicationContext(),sounds[rndm]);
mp1.start();
关于android - 单击按钮播放随机声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21358908/