ComSof-I
ComSof-I

Reputation: 157

all switch cases are being ran

I am trying to write an app that allows the user to play sounds. First the user selects the category of sound and then the specific sound within the category. once the sound is chosen the user presses a button to play the sound.

I have 3 Radio button groups, the first radio button group is the category selection. The other 2 RadioGroups are for the specific sounds. I had to break the specific sound radiogroup into 2 because it was the only way I can fit it into the screen. Switch statements are used to determine which category and which specific sound is selected. All radiogroups are working, as well as the button to play the sound.

The problem is that it is playing multiple sounds from several switch statements. Any tips on how to fix that would be greatly appreciated.

public class Main extends ActionBarActivity {
    private RadioGroup category, topRow, bottomRow;
    private RadioButton animal, people, crashes, explosions, sound1, sound2, sound3, sound4, sound5, sound6;
    private Button player;
    int type;
boolean topPlayed = false;
boolean bottomPlayed = false;

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

        //UI elements
        //Radio button groups
        category = (RadioGroup) findViewById(R.id.soundType);
        topRow = (RadioGroup) findViewById(R.id.topthree);
        bottomRow = (RadioGroup) findViewById(R.id.bottomthree);
        //Radio buttons
        //group category
        animal = (RadioButton) findViewById(R.id.Animals);
        people = (RadioButton) findViewById(R.id.People);
        crashes = (RadioButton) findViewById(R.id.Crashes);
        explosions = (RadioButton) findViewById(R.id.Explosions);
        //group topRow
        sound1 = (RadioButton) findViewById(R.id.Sound1);
        sound2 = (RadioButton) findViewById(R.id.Sound2);
        sound3 = (RadioButton) findViewById(R.id.Sound3);
        //group bottomRow
        sound4 = (RadioButton) findViewById(R.id.Sound4);
        sound5 = (RadioButton) findViewById(R.id.Sound5);
        sound6 = (RadioButton) findViewById(R.id.Sound6);
        // the button
        player = (Button) findViewById(R.id.Player);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // not needed here
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // not needed here
    }
    //select which type of sound to use, set text on hidden radio buttons, and make them visible
    public void categorySelect(View view){
        boolean checked = ((RadioButton) view).isChecked();

        switch (view.getId()){

            case R.id.Animals:
                if (checked)
                    {type = 1;
                    topRow.setVisibility(View.VISIBLE);
                    bottomRow.setVisibility(View.VISIBLE);
                sound5.setVisibility(View.VISIBLE);//some options rehide these
                sound6.setVisibility(View.VISIBLE);

                    sound1.setText("noise1");
                    sound2.setText("noise2");
                    sound3.setText("noise3");
                    sound4.setText("noise4");
                    sound5.setText("noise5");
                    sound6.setText("noise6");}
                break;

            case R.id.People:
                if (checked)
                    {type = 2;
                    topRow.setVisibility(View.VISIBLE);
                bottomRow.setVisibility(View.VISIBLE);
                sound5.setVisibility(View.VISIBLE);//some options rehide these
                sound6.setVisibility(View.VISIBLE);

                sound1.setText("noise1");
                sound2.setText("noise2");
                sound3.setText("noise3");
                sound4.setText("noise4");
                sound5.setText("noise5");
                sound6.setText("noise6");}
                break;

            case R.id.Crashes:
                if (checked)
                    {type = 3;
                    topRow.setVisibility(View.VISIBLE);
                bottomRow.setVisibility(View.VISIBLE);
                sound6.setVisibility(View.INVISIBLE);

                sound1.setText("noise1");
                sound2.setText("noise2");
                sound3.setText("noise3");
                sound4.setText("noise4");
                sound5.setText("noise5");
                sound6.setText("error");}//should not be seen in this category

                break;

            case R.id.Explosions:
                if (checked)
                    {type = 4;
                    topRow.setVisibility(View.VISIBLE);
                bottomRow.setVisibility(View.VISIBLE);
                sound5.setVisibility(View.INVISIBLE);
                sound6.setVisibility(View.INVISIBLE);

                sound1.setText("noise1");
                sound2.setText("noise2");
                sound3.setText("noise3");
                sound4.setText("noise4");
                sound5.setText("error");//should not be seen
                sound6.setText("error");}//should not be seen

                break;
        }

    }
    //used to make 2 different radioGroups work as 1
    public void soundSelect(View view){

        boolean checked = ((RadioButton) view).isChecked();

        switch (view.getId()){

            case R.id.Sound1:
             if (checked)
{topPlayed = true;
bottomPlayed = false;
                    sound1.setChecked(true);
                    sound2.setChecked(false);
                    sound3.setChecked(false);
                    sound4.setChecked(false);
                    sound5.setChecked(false);
                    sound6.setChecked(false);}
                break;

            case R.id.Sound2:
             if (checked)
{topPlayed = true;
bottomPlayed = false;
                    sound1.setChecked(false);
                    sound2.setChecked(true);
                    sound3.setChecked(false);
                    sound4.setChecked(false);
                    sound5.setChecked(false);
                    sound6.setChecked(false);}
                break;
            case R.id.Sound3:
            if (checked)
{topPlayed = true;
bottomPlayed = false;
                    sound1.setChecked(false);
                sound2.setChecked(false);
                sound3.setChecked(true);
                sound4.setChecked(false);
                sound5.setChecked(false);
                sound6.setChecked(false);}
                break;
            case R.id.Sound4:
             if (checked)
{topPlayed = false;
bottomPlayed = true;
                    sound1.setChecked(false);
                sound2.setChecked(false);
                sound3.setChecked(false);
                sound4.setChecked(true);
                sound5.setChecked(false);
                sound6.setChecked(false);}
                break;
            case R.id.Sound5:
            if (checked)
{topPlayed = false;
bottomPlayed = true;
                sound1.setChecked(false);
                sound2.setChecked(false);
                sound3.setChecked(false);
                sound4.setChecked(false);
                sound5.setChecked(true);
                sound6.setChecked(false);}
                break;
            case R.id.Sound6:
             if (checked)
{topPlayed = false;
bottomPlayed = true;
                 sound1.setChecked(false);
                 sound2.setChecked(false);
                 sound3.setChecked(false);
                 sound4.setChecked(false);
                 sound5.setChecked(false);
                 sound6.setChecked(true);}
                break;
        }

    }

    public void playSound (View view){
        String msg;

        /* Was used to test if the type variable was setting right. commented out cause it works- delete before going live
        String msg2 = type;
        Toast.makeText(getApplicationContext(), msg2, Toast.LENGTH_LONG).show();*/


        switch (type){
            case 1:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:

if (topPlayed){msg = "animal first sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                            mp.start();}
                        break;

                    case R.id.Sound2:
if (topPlayed)
                        {msg = "animal second sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                            mp2.start();}
                        break;

                    case R.id.Sound3:
if (topPlayed)
                        {msg = "animal third sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                            mp3.start();}
                        break;
                }
                    switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
if (bottomPlayed)
                        {msg = "animal fourth sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                            mp.start();}
                        break;

                    case R.id.Sound5:
if (bottomPlayed)
                        {msg = "animal fifth sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                            mp2.start();}
                        break;

                    case R.id.Sound6:
if (bottomPlayed)
                        {msg = "animal sixth sound";
                            Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                            MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                            mp3.start();}
                        break;
                }

            case 2:
                switch (topRow.getCheckedRadioButtonId()){

                    case R.id.Sound1:
                    msg = "person first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "person second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "person third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                     MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }

                switch (bottomRow.getCheckedRadioButtonId()){
                    case R.id.Sound4:
                        msg = "person fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound5:
                        msg = "person fifth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound6:
                        msg = "person sixth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }

            case 3:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:
                    msg = "crash first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "crash second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "crash third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }

                switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
                        msg = "crash fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound5:
                        msg = "crash fifth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound6:
                        msg = "should not see this option";
                        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();

                        break;

                }

            case 4:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:
                    msg = "explode first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "explode second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    {MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "explode third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }
                switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
                        msg = "explode fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();

                        break;

                    case R.id.Sound5:
                        msg = "should not see this option";
                        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
                        break;

                    case R.id.Sound6:
                        msg = "should not see this option";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        break;
                }
        }
    }

}

UPDATE: I made changes based on all the answers that were given. I only updated case1:in playSound() in this post, in the app each case got the update and the app now functions as expected. Thanks for the help!

Upvotes: 0

Views: 68

Answers (4)

HexAndBugs
HexAndBugs

Reputation: 5789

The reason you are getting multiple sounds is that you haven't got break statements in your outer level switch statement in the playSound() method.

You've got break statements on the inner level switch statements, but you need to add them immediately before

case 2:

and similarly for case 3: and case 4:`.

I'm also not sure your if statements work the way you expect them to. Your code indentation suggest where you've got:

if (checked)
    sound1.setChecked(true);
    sound2.setChecked(false);
    sound3.setChecked(false);
    sound4.setChecked(false);
    sound5.setChecked(false);
    sound6.setChecked(false);

that you want all of the setChecked methods called only if checked is true, but what it will actually do is only set sound1.setChecked(true); if checked is true and will set all the others regardless of the value of checked. This is because the if only applies to the next line. If you want it to apply to more than just the next line, you need to use curly braces { and } to identify the block to which the if applies. So, if you want it to only set all of them if checked is true, you need to change it to:

if (checked) {
    sound1.setChecked(true);
    sound2.setChecked(false);
    sound3.setChecked(false);
    sound4.setChecked(false);
    sound5.setChecked(false);
    sound6.setChecked(false);
}

UPDATE

I just noticed you also have two switch statements within each case in your outer switch statement so you might also need a flag or something to check if you played a sound from your first radio group and only run the second switch statement if you didn't.

Upvotes: 1

Abhinav Singh Maurya
Abhinav Singh Maurya

Reputation: 3313

Your public void playSound (View view){} consist of switch statement which is switch (type){ case 1:} This switch statement cases does not consist of break; statement which is causing to play multiple sounds. change your method to like below and your code will work like charm.

public void playSound (View view){
        String msg;

        /* Was used to test if the type variable was setting right. commented out cause it works- delete before going live
        String msg2 = type;
        Toast.makeText(getApplicationContext(), msg2, Toast.LENGTH_LONG).show();*/


        switch (type){
            case 1:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:
                     msg = "animal first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                    msg = "animal second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                    msg = "animal third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }
                    switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
                    msg = "animal fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound5:
                    msg = "animal fifth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound6:
                    msg = "animal sixth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }
              break;
            case 2:
                switch (topRow.getCheckedRadioButtonId()){

                    case R.id.Sound1:
                    msg = "person first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "person second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "person third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                     MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }

                switch (bottomRow.getCheckedRadioButtonId()){
                    case R.id.Sound4:
                        msg = "person fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound5:
                        msg = "person fifth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound6:
                        msg = "person sixth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }
            break;
            case 3:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:
                    msg = "crash first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "crash second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "crash third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }

                switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
                        msg = "crash fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound5:
                        msg = "crash fifth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound6:
                        msg = "should not see this option";
                        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();

                        break;

                }
           break;
            case 4:
                switch (topRow.getCheckedRadioButtonId()) {

                    case R.id.Sound1:
                    msg = "explode first sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();
                        break;

                    case R.id.Sound2:
                        msg = "explode second sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    {MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sound);
                        mp2.start();
                        break;

                    case R.id.Sound3:
                        msg = "explode third sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sound);
                        mp3.start();
                        break;
                }
                switch (bottomRow.getCheckedRadioButtonId()){

                    case R.id.Sound4:
                        msg = "explode fourth sound";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
                        mp.start();

                        break;

                    case R.id.Sound5:
                        msg = "should not see this option";
                        Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
                        break;

                    case R.id.Sound6:
                        msg = "should not see this option";
                        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                        break;
                }
           break;
        }
    }

Upvotes: 0

Logic
Logic

Reputation: 2258

The way you are check the RadiButton in a RadioGroup is checked or not the way you are doing. It should be done as follows:

if(gender.getCheckedRadioButtonId()==-1)
{
    Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
}
else
{
    // get selected radio button from radioGroup
    int selectedId = gender.getCheckedRadioButtonId();
    // find the radiobutton by returned id
    selectedRadioButton = (RadioButton)findViewById(selectedId);
    Toast.makeText(getApplicationContext(), selectedRadioButton.getText().toString()+" is selected", Toast.LENGTH_SHORT).show();
}

Upvotes: 0

Sascha Kolberg
Sascha Kolberg

Reputation: 7162

This is java, you cannot mark blocks by indentation alone. So, it is generally a good idea to place curlies around your if blocks, e.g:

change

if (checked)
                sound1.setChecked(true);
                sound2.setChecked(false);
                sound3.setChecked(false);
                sound4.setChecked(false);
                sound5.setChecked(false);
                sound6.setChecked(false);

to

if (checked) {
                sound1.setChecked(true);
                sound2.setChecked(false);
                sound3.setChecked(false);
                sound4.setChecked(false);
                sound5.setChecked(false);
                sound6.setChecked(false);
}

and the other accordingly, then it should work.

Upvotes: 1

Related Questions