I’ve read several topics to this but couldn’t find any good solution for my problem.
I’ve one Checkbox which is standard “checked” with state TRUE. I want know to stop my background music if I’m unchecking it – On the fly with no button. I know that there is something missing but I cant get it! Thanks for ur help!
I’m using this Code which has no errors but it doesn’t works correctly.
private CheckBox chkBoxMusic;
private MediaPlayer mp_music;
OnClickListener checkboxListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sound);
mp_music = MediaPlayer.create(this,R.raw.menusong);
chkBoxMusic = (CheckBox) findViewById(R.id.checkBoxMusic);
checkboxListener = new OnClickListener() {
public void onClick(View v) {
boolean checked = ((CheckBox) v).isChecked();
if (!checked) {
mp_music.stop();
}
}
};
chkBoxMusic.setOnClickListener(checkboxListener);
}
}
Change you code as by using CompoundButton.OnCheckedChangeListener for checkbox: