我想创建一个应用程序,播放随机声音从我的原始资源。它生成一个声音的名称,但是我如何使用这个名称,它是我的字符串变量的值来启动声音。
String[] imena_tonova = {"c3", "c-3", "d3", "d-3", "e3", "f3", "f-3", "g3", "g-3", "a3", "a-3", "b3", "c4", "c-4", "d4", "d-4", "e4", "f4", "f-4", "g4", "g-4", "a4", "a-4", "b4"};
int range2 = 11 - 0 + 1;
int randomNum2 = rn.nextInt(range) + 0;
String pt = imena_tonova[randomNum2];
final MediaPlayer Play = MediaPlayer.create(this, R.raw.);
Play.start();我必须在.create()函数中传递什么作为第二个参数?
发布于 2017-01-13 20:28:29
使用像这样的东西
int resourceId = getResources().getIdentifier(pt, "raw", getPackageName());发布于 2017-01-13 20:33:47
将标识符存储在int[]数组中。
int[] imena_tonova = { R.raw.c3; }或者,如果您需要与资源相关联的名称,可以使用Map。当然,你必须想出随机选择项目的方法。
Map<String, Integer> imena_tonova = new HashMap<>();
// Add items to your map
imena_tonova.put("C3", R.raw.c3);https://stackoverflow.com/questions/41634632
复制相似问题