我想创建一个从我的原始资源中播放随机声音的应用程序。它生成声音的名称,但如何使用该名称(字符串变量的值)来启动声音。
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() 函数中作为第二个参数传递什么?
最佳答案
使用类似的东西
int resourceId = getResources().getIdentifier(pt, "raw", getPackageName());
关于java - 使用字符串变量作为我想要播放的声音的名称,Java (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41634632/