我正在为我的 android 应用程序使用 SoundPool。我在我的主要活动中将大约 75 个一到三秒的声音加载到池中,然后使用如下所示的 Sound 方法从其他活动中引用它们:
public void Sound(int s){
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
MainActivity.spool.play(s, volume, volume, 1, 0, 1f);
};
s 是我在 MainActivity 类中定义的整数,例如:
static int sound_e;
然后像这样传递给 Sound 方法:
Sound(sound_e);
我希望能够定义这样的字符串:
String letter_sound = "MainActivity.sound_" + currentLetter;
//Example value of this string would be MainActivity.sound_a
然后将该字符串作为整数传递给 Sound。这是为了避免 26 个 if 语句,我为这样的数字做了这些:
if (randomNum == 1) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_1);}
else if (randomNum == 2) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_2);}
else if (randomNum == 3) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_3);}
else if (randomNum == 4) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_4);}
else if (randomNum == 5) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_5);}
else if (randomNum == 6) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_6);}
else if (randomNum == 7) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_7);}
else if (randomNum == 8) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_8);}
else if (randomNum == 9) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_9);}
else if (randomNum == 10) {Log.v(TAG, "Sound playing for: " + randomNum); Sound(MainActivity.sound_10);}
else Log.v(TAG, "None of the ifs are true. randomNum: " + randomNum);
我在 android 文档中没有看到任何将字符串值传递到 SoundPool 播放的方法,只是整数。感谢您帮助我弄清楚我错过了什么!