0

How can I stop my MP3 file when the user closing the app?

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
  setContentView(R.layout.activity_fullscreen);
  mySong = MediaPlayer.create(FullscreenActivity.this,R.raw.thuglife);
  mySong.start();
}
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Lior Dahan
  • 29
  • 3
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Josef Adamcik Aug 24 '17 at 11:16
  • Welcome to Stack Overflow, please take a time to go through [the welcome tour](https://stackoverflow.com/tour) to know your way around here (and also to earn your first badge), read how to [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and also check [How to Ask Good Questions](https://stackoverflow.com/help/how-to-ask) so you increase your chances to get feedback and useful answers. – Josef Adamcik Aug 24 '17 at 11:16

2 Answers2

0

Use activity onPause and onStop functions to stop the song before the activity exits. You can make some improvements to this snippet.

@Override
public void onPause() {
  super.onPause();  
   mySong.stop();
 }
Mohale
  • 2,040
  • 3
  • 17
  • 18
0

Use activity onStop for Called when the activity is no longer visible to the user, either because it is being destroyed or because a resumed activity has come in front of it.

@Override
public void onPause() {
  mySong.stop();
  super.onPause();  
 }

Also, you can search The Activity Life Cycle

Cihat Şaman
  • 3,674
  • 4
  • 14
  • 24