2

In my JumbledWords game app, I am providing options to Turn On and Turn Off the sounds. The problem is that I am not able to do that. I have written the code for it but it 's not working.

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();
                }

             });
        }


    }
}

My problem is that if I use the Turn Off option, my sound plays, which must not happen in this case. Please help me.

sohaib rahman
  • 301
  • 1
  • 6
  • 15
  • One potential issue could be that you should be calling `mp.setOnCompletionListener()` **before** `mp.onStart()`. It's possible that you are not properly releasing your MediaPlayer instance. I'm not sure if that is actually causing your problem, though. – Mark Allison Jun 14 '11 at 09:47

3 Answers3

3

You are not actually handling changes to your radio buttons.

You need to define the RadioGroup control which wraps your RadioButton controls, and defines the "group" of radio buttons; then call setOnCheckedChangeListener() on your RadioGroup to define a Listener which will get called when the state changes. It is in this Listener that you need to check the state of the individual buttons and call jw.setSoundOn(); rather than doing it in your onCreate() method:

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);
            }
        }
    } );
}
Mark Allison
  • 21,839
  • 8
  • 47
  • 46
0

Ain't sure where the problem lays, but found some problems in your code: 1) Don't create objects when initializing them:

JumbledWords jw = new JumbledWords();

is not right. You should initialize the variable, and then call constructor in activity's onCreate:

JumbledWords jw;

And inside onCreate():

jw = new JumbledWords();

2) If soundOn is declared as static, every method using this variable should also be static. And you should call those methods in a static way:

JumbledWords.setSoundOn(true);

Try fixing these issues, and maybe you'll resolve your problem. Good luck!

Egor
  • 39,695
  • 10
  • 113
  • 130
0

Check out the code I wrote in this post. You can get an idea of how to use the media and ringtone volume and show the user you are changing it.

How do you get/set media volume (not ringtone volume) in Android?

Community
  • 1
  • 1
Anthony Graglia
  • 5,355
  • 5
  • 46
  • 75