ImageButton Soundboard android app

前端 未结 1 757
北荒
北荒 2021-01-16 18:58

I\'m just starting out with my first soundboard. Basically this is what I have so far (except I have 40 sounds). Does anyone know a better way to do this? I have to go to an

相关标签:
1条回答
  • 2021-01-16 19:34

    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.

    0 讨论(0)
提交回复
热议问题