Dans mon application de jeu JumbledWords, je fournis des options pour activer et désactiver les sons. Le problème est que je ne suis pas capable de faire cela. J'ai écrit le code pour ça mais ça ne marche pas.comment activer et désactiver le son par programmation dans 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();
}
});
}
}
}
Mon problème est que si j'utilise l'option Désactiver, mes jeux sonores, qui ne doit pas se produire dans ce Cas. Aidez-moi, s'il vous plaît.
Un problème potentiel pourrait être que vous devriez appellerez 'mp.setOnCompletionListener() '** avant **' mp.onStart() '. Il est possible que vous ne libérez pas correctement votre instance MediaPlayer. Je ne suis pas sûr si cela cause réellement votre problème, cependant. –