1

I'm trying to do some simple apps like click button, play sound. In my last App I did it all with switch cases but I realized that if I have a lot of codes I have to write a lot of code for each button one case. Now I am trying to save my buttons and my sounds into Arrays and looping them in a double for.

I also did some context menu which is working fine. But my problem now is the playing the sounds. Actually it plays the sound2 for both buttons. It should play for button1 -> sound1 and for button2 -> sound2

Can somebody find my Problem here?

public class MainActivity extends AppCompatActivity {

    MediaPlayer MainMedia;

    public int [] buttonsas = {R.id.button1, R.id.button2};
    public int [] sounds = {R.raw.sound1, R.raw.sound2};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for (int i=0; i<buttonsas.length; i++) {
            Button contextMenuButton = (Button) findViewById(buttonsas[i]) ;
            registerForContextMenu(contextMenuButton);
        }

        MainMedia = MediaPlayer.create(this, R.raw.sound1);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.item_option1:
            case R.id.item_option2:
                Toast.makeText(this, item.toString(), Toast.LENGTH_LONG).show();
                break;
            case R.id.item_option3:
                Toast.makeText(this, item.toString(), Toast.LENGTH_LONG).show();
                break;
        }

        return super.onContextItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.main_menu, menu);
    }

    public void MainMedia (View view) {
        for (int i=0; i<buttonsas.length; i++) {
            for (int j=0; j<sounds.length; j++) {
                MainMedia.release();
                MainMedia = MediaPlayer.create(MainActivity.this, sounds[j]);
                MainMedia.start();
            }
        }
    }
}
2
  • why don't you create a method and pass sound file path with onclick listener so that your code would be cleaner? Commented Feb 24, 2017 at 10:15
  • Hello since Im a bit new to Java. Do you have a little Tutorial of that ? Commented Feb 24, 2017 at 10:19

2 Answers 2

2

If I understand your code correctly, in the method MainMedia it IS actually playing both sounds for both buttons, but it only seems as though it is playing the second one because the first one is released almost immediately.

I honestly think there are much neater and less processing solutions, but if you want to do it with two nested for then I think a solution could be:

Try putting an if statement after the second for so you can know which button you are pressing:

public void MainMedia (View view) {
    for (int i=0; i<buttonsas.length; i++) {
        for (int j=0; j<sounds.length; j++){
            if (view.getId() == buttonsas[i] && i == j) {
                MainMedia.release();
                MainMedia = MediaPlayer.create(MainActivity.this, sounds[j]);
                MainMedia.start();
            }
        }
    }
}

Though I think this solution should work, I recommend doing something like this, better:

public void MainMedia (View view) {
    // Traverse the buttonsas array to get the item the user just pressed.
    for (int i=0; i<buttonsas.length; i++) {
        // Check whether the item we got from the array is equal to the item we received as a parameter
        if (view.getId() == buttonsas[i]) {
            MainMedia.release();

            // Since the sounds and buttons have a relation in the same position in two different arrays,
            // just get the position we have depending on the selected item.
            MainMedia = MediaPlayer.create(MainActivity.this, sounds[i]);
            MainMedia.start();
        }
    }
}

That way you will save a for loop.

Let me know if this helps.

EDIT : I have added comments in the code so it is easier to follow. In a nutshell, it works because you have a relation of item - sound in two separate arrays, so item1 (R.id.button1) is in position1 (buttonsas[1]) of array1 (buttonsas), that way, when I get the position of the item I want, I just use that same position on the array2 (sounds[1]). Since you are using an iterator (for loop) the position is the i.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh thanks alot :) Both methods work but I thought I have to use it in a double loop to get each position for sound. Now I want to ask you about the recommend one. I can read in this for loop he picks Button 1, 2 ,3 etc but how you explain that button 1 -> sound1(play) ?
Oh thanks dude this helped me alot. And I got one more Question. So I also build a context menu because I wanted to use it with the Option1 if (view.getId() == buttonsas[i] && i == j) <- but this is not working since I have no view there. Is there a way to solve this or better stay at method on button click?
@ExiizZ - I am sorry but I do not know much about Android so I am not able to help you there. My suggestion is to try it as much as you can until it works the way you want it to work or you as satisfied with it. Probably it would be wise to post another question with that new issue you have and tag Android on the question. Good luck with it!
0

The problem is in your loop. You are actually starting all sounds at the same time. What are trying to achieve with that?

1 Comment

Hello as I said on Top. For Example I have 3 buttons and 3 sounds files. I want to set for button1 - sound1, button2 - sound 2, button3 - sound3. So

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.