1

我在原始文件夹中有大约 100 个音频文件,这 100 个文件只是说从 1 到 100 的数字。

在我点击一个按钮之后。我希望它根据文本框中输入的文本播放号码。

所以我使用声音池和哈希图。这工作得很好,没有任何问题

soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
soundsMap = new HashMap<Integer, Integer>();
soundsMap.put(1, soundPool.load(this, R.raw.number1, 1));
soundsMap.put(2, soundPool.load(this, R.raw.number2, 1)); 

像上面一样,直到 number100。我获取文本字段的值

我放了一个从 1 到 100 的 for 循环。将播放什么值。

使用 soundPool.play(1,....)

这工作得很好,没有任何问题。

但奇怪的是,因为它正在创建 100 值的 haspmap.. 有 5 秒的延迟和空白屏幕,我无法对进程进行任何操作。

有什么方法可以根据值创建哈希图并播放声音。

就像说例如

如果文本字段的值为 20

in the for loop: i=1 to 100..
it should get the value 20:
string a = "R.raw.number"+i
SoundsMap.put(1, soundPool.load(this, a, 1));
soundPool.play(1,....)

如果上述是可能的......我该怎么做?

等待你的回复!谢谢!

4

1 回答 1

1

为什么要在开始时初始化所有内容?如果用户从不点击多个按钮怎么办?我建议在您的按钮单击时加载,如下所示:

//in onCreate()

final EditText editText = (EditText)findViewById(R.id.my_input_field);
int[] sounds = { R.raw.number1, R.raw.number2,
                 R.raw.number3, /*etc.*/ };

//in your onClick()

try {
    Double value = Double.parseDouble(editText.getText());
    int soundId = soundPool.load(MyActivity.this, sounds[value - 1], 1);
    soundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
} catch (NumberFormatException e) {
    //Alert the user that an invalid number was entered
}
于 2012-06-07T20:46:31.233 回答