我正在尝试编写一个允许用户播放声音的应用。首先,用户选择声音类别,然后选择类别中的特定声音。一旦选择了声音,用户按下按钮即可播放声音。
我有3个单选按钮组,第一个单选按钮组是类别选择。其他2个RadioGroup用于特定声音。我不得不将特定声音radiogroup分解为2,因为这是我可以将它放入屏幕的唯一方法。 Switch语句用于确定选择哪个类别和特定声音。所有的无线电组都在工作,以及播放声音的按钮。
问题是它正在从多个switch语句中播放多个声音。关于如何解决这个问题的任何提示都将非常感激。
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;
}
}
}
}
更新
我根据给出的所有答案进行了更改。我在这篇文章中仅更新了case1:
中的playSound()
,在应用程序中,每个案例都获得了更新,现在应用程序按预期运行。谢谢你的帮助!
答案 0 :(得分:1)
这是java,你不能仅通过缩进来标记块。 因此,在if块周围放置curlies通常是个好主意,例如:
更改
if (checked)
sound1.setChecked(true);
sound2.setChecked(false);
sound3.setChecked(false);
sound4.setChecked(false);
sound5.setChecked(false);
sound6.setChecked(false);
到
if (checked) {
sound1.setChecked(true);
sound2.setChecked(false);
sound3.setChecked(false);
sound4.setChecked(false);
sound5.setChecked(false);
sound6.setChecked(false);
}
和另一个相应的,然后它应该工作。
答案 1 :(得分:1)
您获得多个声音的原因是您在break
方法的外层switch
语句中没有playSound()
个语句。
您在内级break
语句中有switch
个语句,但您需要在
case 2:
,类似于case 3:
和案例4:`。
我也不确定您的if
语句是否按预期方式运行。您的代码缩进建议您获得的位置:
if (checked)
sound1.setChecked(true);
sound2.setChecked(false);
sound3.setChecked(false);
sound4.setChecked(false);
sound5.setChecked(false);
sound6.setChecked(false);
您希望仅在setChecked
为真时调用所有checked
方法,但如果sound1.setChecked(true);
为真,则实际执行的内容仅设置为checked
无论checked
的值如何,都设置所有其他值。这是因为if
仅适用于下一行。如果您希望它不仅仅适用于下一行,则需要使用花括号{
和}
来标识if
适用的块。因此,如果您希望仅在checked
为真时才设置所有这些,则需要将其更改为:
if (checked) {
sound1.setChecked(true);
sound2.setChecked(false);
sound3.setChecked(false);
sound4.setChecked(false);
sound5.setChecked(false);
sound6.setChecked(false);
}
<强>更新强>
我刚刚注意到您在switch
语句中的每个case
内都有两个switch
语句,因此您可能还需要一个标记或其他内容来检查您是否播放了声音第一个广播组,如果你没有,则只运行第二个switch
语句。
答案 2 :(得分:0)
您检查RadiButton
中的RadioGroup
的方式是否已按照您的方式进行检查。它应该按如下方式完成:
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();
}
答案 3 :(得分:0)
您的public void playSound (View view){}
由switch语句组成,switch (type){ case 1:}
此switch语句不包含导致播放多个声音的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:
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;
}
}