mardi 10 mai 2016

sound check box deactivate android

Iam creating a simple game in android,I want to start with splash sound and back music,when l want to deactivate the sound with check box, it not working and the music still on.when app start it splash with music and transfer to another music when it transfer to menu.

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

            }}  });}}

splash.java

public class Splash extends Activity {
public MediaPlayer splash;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        splash = MediaPlayer.create(Splash.this, R.raw.splash);
        splash.start();
        splash.setOnCompletionListener(new OnCompletionListener(){

            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                MediaPlayer next = MediaPlayer.create(Splash.this, R.raw.backmusic);
                next.start();

            }

        });
    }

    @Override
    protected void onStart()
    {
        Log.i("splash", "onStart called");
        super.onStart();
         setContentView(R.layout.splash);

            Thread timer = new Thread(){
                public void run(){
                    try{
                        sleep(3000);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }finally{
                        Intent showGameMenu = new Intent(Splash.this,Menu.class);
                        startActivity(showGameMenu);
                    }
                }
            };
            timer.start();


    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
        super.onConfigurationChanged(newConfig);
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
          setContentView(R.layout.splash);

            Thread timer = new Thread(){
                public void run(){
                    try{
                        sleep(3000);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }finally{
                        Intent showGameMenu = new Intent(Splash.this,Menu.class);
                        startActivity(showGameMenu);
                    }
                }
            };
            timer.start();}}




Aucun commentaire:

Enregistrer un commentaire