Search code examples
javaandroidandroid-mediaplayer

Music doesn't play again after I press home or recents button


I have an app with a button to play or pause music. Pressing the back button while playing music pauses it and opening the app again resumes the music after pressing the play button. But that doesn't work with home or recents button. The music pauses but upon reopening the app and pressing the play button doesn't play the music until a force close. Here is the code:

package com.example.firozkaoo2222.myapplication;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import static com.example.firozkaoo2222.myapplication.R.raw.police;

public class MainActivity extends AppCompatActivity {

private MediaPlayer policeSound = MediaPlayer.create(this, police);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button policeSounds = this.findViewById(R.id.police);

    policeSounds.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (policeSound == null) {
                policeSound = MediaPlayer.create(getApplicationContext(), R.raw.police);
            }

            if (policeSound.isPlaying()) {
                policeSound.pause();
            } else {

                policeSound.start();
            }
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    if (policeSound != null) {
        policeSound = MediaPlayer.create(this, R.raw.police);
        policeSound.start();
    }

}

@Override
public void onPause() {
    super.onPause();
    if (policeSound.isPlaying())
        policeSound.pause();
}

//Back button pressed.
@Override
public void onBackPressed() {
    super.onBackPressed();
    if (policeSound.isPlaying())
        policeSound.pause();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    policeSound.stop();
    policeSound = null;
}

}


Solution

  • If you press home button the app go to the background and when you came from the background you should override the method onResume();

    Code:

    public class MainActivity extends AppCompatActivity {
    
        //THIS IS YOUR LAST MISTAKE. IF YOU TRY TO CREATE THE OBJECT WITH THE CONTEXT AND THE RESOUERCES
        //WHEN THE ACTIVITY IS NOT CREATED YET, YOUR APP CRASH
        //private MediaPlayer policeSound = MediaPlayer.create(this, R.raw.police);
        private MediaPlayer policeSound;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button policeSounds = this.findViewById(R.id.police);
    
            policeSounds.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (policeSound == null) {
                        policeSound = MediaPlayer.create(getApplicationContext(), R.raw.police);
                    }
    
                    if (policeSound.isPlaying()) {
                        policeSound.pause();
                    } else {
    
                        policeSound.start();
                    }
                }
            });
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            if (policeSound != null) {
                policeSound = MediaPlayer.create(this, R.raw.police);
                policeSound.start();
            }
    
        }
    
        @Override
        public void onPause() {
            super.onPause();
            if (policeSound.isPlaying())
                policeSound.pause();
        }
    
        //Back button pressed.
        @Override
        public void onBackPressed() {
            super.onBackPressed();
            if (policeSound.isPlaying())
                policeSound.pause();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            policeSound.stop();
            policeSound = null;
        }
    }
    

    I hope this help you.