2011-06-14 74 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); 
      } 
     } 
    }); 
} 
相關問題