I have an application that plays music. (Also you can close on options menu.)
But the problem is that when you press the home button, the music keeps playing, I want to stop the music when the user presses the HOME Button.
I tried to put onPause() and onStop() method to music.stop(). But it fails because when I start a new activity, the music also stops but I don't want it to, only when the Home button is pressed.
My code is:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
it=(RadioButton)findViewById(R.id.radio0);
ti=(RadioButton)findViewById(R.id.radio1);
but=(ToggleButton)findViewById(R.id.toggleButton1);
music=MediaPlayer.create(Options.this, R.raw.avril);
mainmenu=new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC");
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(but.isChecked()) {
if(!music.isPlaying())
music.start();
}
else
if(music.isPlaying())
music.pause();
}
});
it.setChecked(true);
Button menu=(Button)findViewById(R.id.button1);
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(mainmenu);
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
startActivity(new Intent(mainmenu));
}
@Override
protected void onPause() {
if(music.isPlaying()){
music.pause();
music.release();
}
but.setChecked(false);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
Here The OPTIONS ACTIVITY where the music starts to play, But when I pass the menu activity, the music also stops.