开发者

how to set sound on and off programmatically in android

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_scr开发者_如何学Pythoneen);

    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.


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


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!


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜