onDestroy crash after closing app - android

I'm having some problems after making an override of the method onDestroy.
My app is a music player, using the instance of mediaplayer I need at some point to force the release of it if no music is playing.
This is my code so far, for making the trick I've both overrided the onKeyDown() and onDestroy() method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(mp.isPlaying())
{
//Genera la notifica
generateNotificationSong();
//Muovi in background
moveTaskToBack(true);
}
else finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
//Faccio un override della funzione onDestroy per evitare che il mediaplayer continui
//a mandare musica in background, inoltre l'UpdateTimeTask risulta inutile
#Override
public void onDestroy()
{
mNotify.cancel(001);
if(mHandler != null)
mHandler.removeCallbacks(mUpdateTimeTask); //rimuovo il thread che aggiorna la seekbar
if(mp != null)
mp.release(); //rilascio il media player
super.onDestroy();
}
That's it, now when I want to close the application I simply press the back button and the apps calls the methods onPause() onStop() and onDestroy() right?
Anyway sometimes happens that after the closing the phone freeze for 4-5 seconds and a message is displayed: "The program Application has been closed".
I know I'm doing something wrong here but I don't know what, I need some help.
Thanks in advice!

super.onDestroy() must be the first call of the onDestroy method if you override it.

Try the below code it works for me
#Override
public void onDestroy() {
mediaPlayer.stop();
super.onDestroy();
}
}

Related

Releasing Media Player while preparing is causing app to freeze

I have two activities a main activity which has Recyclerview and a detailedActivity which is launched every time the user clicks on one of the items of the Recyclerview. The detailedActivity has a mediaplayer component that is being created everytime a detailedActivity is created. Now in the onDestroy method I always free the resources taken by the mediaPlayer by this code:
#Override
protected void onDestroy() {
if (mMediaPlayer != null) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();}
mMediaPlayer.release();
mMediaPlayer=null;
}
super.onDestroy();
}
The app freezes for a while every time I click the back button while the mediaplayer is still preparing. The message that I get in the logcat is this:
I/Choreographer: Skipped 112 frames! The application may be doing too much work on its main thread.
So this freezing only happens if I destroyed the activity while it is preparing but if it is already in the prepared state it won't happen. I use prepreAsync to fetch the media from the internet.
Thanks. Any help is highly appreciated. I have been stuck in this problem for days!
OK. I've kind of worked around the problem. I am writing this for anyone who might encounter the same situation as I did. I made two boolean flags in the scope of the class like this:
boolean prepared = false;
boolean cancel = false;
After that in the onpreapred method I set prepared to true.
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
prepared = true;
}
});
In the onDestroy method I check whether the mediaplayer is already prepared or not if prepared I release it from the method its self.
#Override
protected void onDestroy() {
if (mMediaPlayer != null) {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
if (prepared) {
mMediaPlayer.release();
mMediaPlayer = null;
}
cancel = true;
}
super.onDestroy();
}
Otherwise, I set cancel to true and implement on OnBufferingUpdateListener interface and override its method and release the mediaplayer from there.
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if (cancel) {
mp.reset();
mp.release();
mMediaPlayer = null;
Log.i("msg", " mp released");
}
}
Try to remove rechecking if mediaplayer is running or not.
#Override
protected void onDestroy() {
if (mMediaPlayer != null) {
mMediaPlayer.stop();
mMediaPlayer.release();
}
super.onDestroy();
}
If it still happens, try to remove checking mediaplayer in your onDestroy().
-- UPDATE --
This could be related with this bug:
https://code.google.com/p/android/issues/detail?id=959
This could be a help:
Android MediaPlayer reset freezes UI

Audio is playing twice in Android Media Player

In my application I'm having a method responsible for playing file placed under raw directory. But when Ever I call that function in my onResume() method, sound is played twice. Even I have googled and tried different solutions. even by checking mediaPlayer.isPlaying() and then stopping the MediaPlayer instance but still didn't get any help.
private void EnglishSound(){
if(mediaPlayer1!=null){
if(mediaPlayer1.isPlaying()){
mediaPlayer1.stop();
}
mediaPlayer1.reset();
mediaPlayer1.release();
}
mediaPlayer1 = MediaPlayer.create(this, R.raw.p012);
mediaPlayer1.start();
}
public void onResume() {
super.onResume();
EnglishSound();
}
and EnglishSound() is called no where else in the whole activity. Even have tried debugging but it never enters the if block containing isPlaying().
Try to release onPause()
public void onPause() {
super.onPause();
if(mediaPlayer1 != null)
mediaPlayer1.release();
}

Stop music player on device home button android

I have an android application in which am try to play a background sound for an particular activity(sound will play on single activity not for an whole application).
Am using this code to start the MediaPlayer
MediaPlayer backMP = MediaPlayer.create(this, R.raw.theme_loop);
backMP.setLooping(true);
backMP.start();
It is working fine but I just want to stop the music on home button press for this I have try
backMP.release(), backMp.stop() in onPause() method nothing is work for me.
use backMP.stop in both onDestroy,onStop,and onPause it will solve your problem
use this way
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.d("Test", "Home button pressed!");
backMP.stop();
}
return super.onKeyDown(keyCode, event);
}
or you can use like that start playing in onResume()
#Override
protected void onResume() {
super.onResume();
backMP.start();
}
and stop in onpouse() like that
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
backMP.stop();
}

force close if mediaplayer is not used and back button pushed

In an activity I'm using two media players for different sounds, if both sounds are played and the back button is pushed there is no problem, it works fine. the media players stop and release.(mp is set to loop, mps just plays a short sound)
#Override
protected void onPause() {
// TODO Auto-generated method stub
mp.stop();
super.onPause();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
mp.release();
mps.release();
super.onStop();
}
But, if one and/or both sounds are not played I get a forced close when the back button is pushed and a null pointer exception. How would you write code to check if the mediaplayers were ever used and therefore need to stop and release them?
If you are getting a null pointer in your onPause and onStop you need to check if mp is null.
#Override
protected void onPause() {
if (mp != null)
mp.stop();
super.onPause();
}
#Override
protected void onStop() {
if (mp != null)
mp.release();
if (mps != null)
mps.release();
super.onStop();
}
You can keep a count if you would like to but that means keeping track of another variable when you already have access to the variables you need
could you use a count and add one to it if there has been a play then check if it equals 0 or not.

Android: Stop playback on Home or Back button

When I press back or home in my application, it hides, but the MediaPlayer keeps going. Is there a way to know when these buttons have been pressed and stop playback before closing?
just implement this method in your class where media player is calling...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{ //Back key pressed
//Things to Do
if(mediaPlayer!= null)
{
mediaPlayer.stop();
mediaPlayer=null;
}
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
You should be able to override the onPause() function in your Activity. onPause() will be called when you Activity is hidden and you can pause the MediaPlayer there.
For example,
#Override
protected void onPause() {
super.onPause(); // Don't forget this line
myMediaPlayer.pause() // Or whatever the function is to pause it
}
Don't forget to add super.onPause; if you forget it will cause a FC.

Categories

Resources