2011-06-14 75 views
1

在我的JumbledWords游戏应用程序中,我提供了打开和关闭声音的选项。问题是我无法做到这一点。我为它编写了代码,但它不起作用。如何在Android中以编程方式设置声音开启和关闭

SplashScreen.java

RadioButton rbSoundOn, rbSoundOff; 
JumbledWords jw = new JumbledWords(); 
@Override 
public void onCreate(Bundle bundle) 
{ 
    super.onCreate(bundle); 

    //set the full screen view of the activity 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.splash_screen); 

    rbSoundOn = (RadioButton)findViewById(R.id.optSoundOn); 
    rbSoundOff = (RadioButton)findViewById(R.id.optSoundOff); 

    if(rbSoundOn.isChecked() == true) 
    { 
     jw.setSoundOn(true); 
    } 
    else 
    { 
     jw.setSoundOn(false); 
    }} 

JumbledWords.java

static boolean soundOn; 
public void setSoundOn(boolean soundOn) 
{ 
    this.soundOn = soundOn; 
} 

public boolean isSoundOn() 
{ 
    return soundOn; 
} 

public void checkWord() 
{ 
    if(abcd.equalsIgnoreCase(etGuessedWord.getText().toString())) 
    { 
     WordLibrary.setMyInt(WordLibrary.getMyInt() + 10); 
     tvScore.setText(String.valueOf(WordLibrary.getMyInt())); 
     if(soundOn == true) 
     { 
     mp = MediaPlayer.create(this, R.raw.clap); 
     mp.start(); 

     mp.setOnCompletionListener(new OnCompletionListener(){ 

      @Override 
      public void onCompletion(MediaPlayer arg0) { 
       // TODO Auto-generated method stub 
       mp.release(); 
      } 

     }); 
     } 
    } 
    else 
    { 
     if(soundOn == true) 
     { 
     mp = MediaPlayer.create(this, R.raw.oop); 
     mp.start(); 

     mp.setOnCompletionListener(new OnCompletionListener(){ 

       @Override 
       public void onCompletion(MediaPlayer arg0) { 
        // TODO Auto-generated method stub 
        mp.release(); 
       } 

      }); 
     } 


    } 
} 

我的问题是,如果我使用关闭选项,我的声音响起,这绝不能在这种情况发生案件。请帮帮我。 1)初始化时,他们不要创建对象:

JumbledWords jw = new JumbledWords(); 

是不正确的

+0

一个潜在的问题可能是你应该调用'mp.setOnCompletionListener() '**之前**'mp.onStart()'。您可能无法正确释放MediaPlayer实例。不过,我不确定这是否真的会导致你的问题。 – 2011-06-14 09:47:45

回答

0

那里的问题奠定了,却发现在你的代码中的一些问题是不知道。你应该初始化变量,然后调用构造函数在活动的onCreate:

JumbledWords jw; 

而且里面onCreate()

jw = new JumbledWords(); 

2)如果soundOn被声明为静态的,使用这个变量的每个方法也应该是静态的。你应该以静态的方式调用这些方法:

JumbledWords.setSoundOn(true); 

试着解决这些问题,也许你会解决你的问题。祝你好运!

3

你没有实际处理改变你的单选按钮。

您需要定义包裹RadioButton控件的RadioGroup控件,并定义单选按钮的“组”然后在你的RadioGroup上调用setOnCheckedChangeListener()来定义一个监听器,当状态改变时它将被调用。正是在这样的监听器,你需要检查各个按钮的状态,并呼吁jw.setSoundOn();,而不是你的onCreate()方法做:

public void onCreate(Bundle bundle) 
{ 
    super.onCreate(bundle); 

    //set the full screen view of the activity 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.splash_screen); 
    RadioGroup group = (RadioGroup)findViewById(R.id.optSoundGroup); 
    final RadioButton rbSoundOn = (RadioButton)findViewById(R.id.optSoundOn); 
    final RadioButton rbSoundOff = (RadioButton)findViewById(R.id.optSoundOff); 
    group.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
     void onCheckedChanged(RadioGroup group, int checkedId) 
     { 
      if(rbSoundOn.isChecked() == true) 
      { 
       jw.setSoundOn(true); 
      } 
      else 
      { 
       jw.setSoundOn(false); 
      } 
     } 
    }); 
} 
相关问题