【问题标题】:Sound Pool and Hash Map声音池和哈希图
【发布时间】:2012-06-08 04:46:31
【问题描述】:

我在原始文件夹中有大约 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 秒的延迟和空白屏幕,我无法对进程进行任何操作。

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

例如说例如

如果文本字段的值为 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,....)

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

等待您的回复! 谢谢!

【问题讨论】:

  • 我不知道你为什么觉得这很奇怪。您正在加载 100 个音频文件。这需要时间。您可以在后台线程中执行此操作(可能使用AsyncTask)并在加载时显示进度条。我不清楚你用你的 for 循环做什么。
  • 天哪,你是在循环 100 次测试 if(editText.getText().equals(String.valueOf(i))) 吗?诺诺诺诺。将输入解析为一个数字,然后从那里开始。检查下面的示例。

标签: android soundpool


【解决方案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
}

【讨论】:

  • 谢谢...此代码有效,只是我必须将 onLoadListener 与 if 条件结合使用。谢谢你的帮助! :)
猜你喜欢
  • 2013-03-21
  • 2019-09-29
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
  • 2013-12-08
  • 2022-12-10
相关资源
最近更新 更多