ImageButton Soundboard android app

六月ゝ 毕业季﹏ 提交于 2019-12-01 14:39:23

With forty buttons you need to arrange for this to happen in a loop.

Although there are even more clever ways to do this, you can start by building a Map:

Map<Integer, Integer> map = new HashMap<Integer, Integer>>();
map.put(R.id.button1, R.raw.sound1);
map.put(R.id.button2, R.raw.sound2);
   ...

and then iterate:

for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
    final MediaPlayer sound = MediaPlayer.create(entry.getValue());
    Button button = (ImageButton) findViewById(entry.getKey());
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sound.start();
        }
    });
}

This will give you a taste of a looping solution. You also need to consider how you manage your MediaPlayer instances.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!