我在Android中创建一个简单的游戏,当l opn应用程序和声音启动时,我想要通过复选框停用声音,它可以取消激活并显示音乐已停用但音乐仍然打开,任何帮助都是赞赏。
public class Setting extends Activity {
public static MediaPlayer Sounds;
private CheckBox sound;
private Boolean isChecked = false;
public void Is_checked() {
if (isChecked) {
Sounds.start();
Toast.makeText(Setting.this, "Sound is activated ",
Toast.LENGTH_LONG).show();
}
else {
Sounds.stop();
Toast.makeText(Setting.this, "Sound is deactivated ",
Toast.LENGTH_LONG).show();
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
Sounds = MediaPlayer.create(this, R.raw.backmusic);
isChecked = false;
addListenerOnsound();
}
protected void onStart() {
super.onStart();
isChecked = false;
addListenerOnsound();
}
private void save(final boolean isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check", isChecked);
editor.commit();
}
private boolean load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean("check", false);
}
protected void onReStart() {
super.onRestart();
sound.setChecked(load());
}
@Override
void onPause() {
super.onPause();
save(sound.isChecked());
}
@Override
public void onResume() {
super.onResume();
sound.setChecked(load());
}
public void addListenerOnsound() {
sound = (CheckBox) findViewById(R.id.sound);
sound.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
isChecked = true;
Is_checked();
}
if (!((CheckBox) v).isChecked()) {
isChecked = false;
Is_checked();
}} });}}
答案 0 :(得分:0)
如果您想恢复音频,只需使用
即可Sounds.pause();
而不是
Sounds.stop()
但如果你想从头开始,那就去做吧。
public void Is_checked() {
if (isChecked) {
Sounds.start();
Toast.makeText(this, "Sound is activated ",
Toast.LENGTH_LONG).show();
}
else {
Sounds.stop();
Sounds = MediaPlayer.create(this, R.raw.backmusic);
Toast.makeText(this, "Sound is deactivated ",
Toast.LENGTH_LONG).show();
}
}