我爲我的android應用程序使用SoundPool。我加載約75一到三個第二聲音入池在我的主要活動,然後從使用Sound方法,它看起來像這樣的其他活動引用它們:在SoundPool中使用字符串play
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_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發揮,只是整數。謝謝你幫我弄清楚我錯過了什麼!
要做到你想要的東西,我認爲你需要反思,但我也認爲還有其他方法可以做到這一點(常規方法調用?)。我現在可以說的是,你可以交換其他的東西 - 如果是用於switch-case語句,並且不需要重複'Log.v'語句 – keyser
else-ifs在用於不同語音選擇的switch語句中。我不確定嵌套switch語句。 –
你可以簡單地把它放在一個單獨的方法。 – keyser