Reputation: 12735
I write an app for android and there is a music plays in background. When I close application music keeps playing. What is the reason? Why everything belong to app don't close automatically? Should I stop everything manually?
Upvotes: 2
Views: 5739
Reputation: 843
This code worked for me!
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, R.raw.beep_warning);
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBox.isChecked()) {
mediaPlayer.start();
mediaPlayer.setLooping(true);
}
else{
mediaPlayer.pause();
}
}
});
}
@Override
protected void onStop() {
super.onStop();
mediaPlayer.release();
}
Upvotes: 0
Reputation: 707
Yes, you have to stop it manually. Are you using MediaPlayer? I think the API docs indicate you'll need to release the player once you're finish with it. So in your onDestroy do a music.release();
Upvotes: 1
Reputation: 517
I think this funny post by Tim Bray from the Android Developers blog explains clearly when you should stop music and when (and how) you should resume it.
Upvotes: 2
Reputation: 5359
Playing media can be tricky, and depends a lot on how you set things up. You should really read the documentation found here and here. I don't know how you set up your app so I can't give you specifics, but I suspect you're running it through a service ("in the background") in which case it is somewhat autonomous from the Activity and needs to be managed when you leave the app.
Upvotes: 1
Reputation: 48871
Please read Application Fundamentals to understand that an Android 'app' can be made up of many different parts. An Activity
, for example, is NOT the complete app
and when you leave that Activity
to go back to the Home page on your Android device, you are not closing the app.
In answer to your question...
Should I stop everything manually?
No, normally you would let the OS tidy things up but in the case of your music playing then yes, you should stop whatever you have started to play the music.
Upvotes: 2