I'm new to Android, so I have a problem. In Android I want to play background music as soon as my music player starts and have it continue even if the activity changes from one to another. I've tried this code:
MediaPlayer music = MediaPlayer.create(MainActivity.this, R.drawable.bgscore);
music.start();
However, the sound should stop when user closes the app, and it doesn't. How can I achieve this?
Create a separate class for handling several conditions
import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
public class MusicManager {
static final int MUSIC_PREVIOUS = -1;
private static final String TAG = "MusicManager";
static MediaPlayer mp;
private static int currentMusic = -1;
private static int previousMusic = -1;
public static void start(Context context, int music) {
start(context, music, false);
}
public static void start(Context context, int music, boolean force) {
if (!force && currentMusic > -1) {
// already playing some music and not forced to change
return;
}
if (music == MUSIC_PREVIOUS) {
Log.d(TAG, "Using previous music [" + previousMusic + "]");
music = previousMusic;
}
if (currentMusic == music) {
// already playing this music
return;
}
if (currentMusic != -1) {
previousMusic = currentMusic;
Log.d(TAG, "Previous music was [" + previousMusic + "]");
// playing some other music, pause it and change
pause();
}
currentMusic = music;
Log.d(TAG, "Current music is now [" + currentMusic + "]");
if (mp != null) {
if (!mp.isPlaying()) {
mp.start();
}
} else {
mp = MediaPlayer.create(context, R.raw.backGroundMusic); //Ur BackGround Music
}
if (mp == null) {
Log.e(TAG, "player was not created successfully");
} else {
try {
mp.setLooping(true);
mp.start();
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
public static void pause() {
if (mp != null) {
if (mp.isPlaying()) {
mp.pause();
}
}
// previousMusic should always be something valid
if (currentMusic != -1) {
{
previousMusic = currentMusic;
Log.d(TAG, "Previous music was [" + previousMusic + "]");
}
currentMusic = -1;
Log.d(TAG, "Current music is now [" + currentMusic + "]");
}
}
public static void release() {
Log.d(TAG, "Releasing media players");
try {
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
}
mp.release();
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
if (currentMusic != -1) {
previousMusic = currentMusic;
Log.d(TAG, "Previous music was [" + previousMusic + "]");
}
currentMusic = -1;
Log.d(TAG, "Current music is now [" + currentMusic + "]");
}
}
Then in your MainActivity
define a global boolean variable and set it to true
before setContentView(....) in onCreate()
i.e
boolean continueBGMusic;
....
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
continueBGMusic=true;
setContentView(R.layout.activity_main);
.....
}
Then update onPause()
as
public void onPause()
{
super.onPause();
if(!continueBGMusic)
MusicManager.pause();
}
and onResume()
as
public void onResume()
{
super.onResume();
continueBGMusic=false;
MusicManager.start(this,R.raw.backGroundMusic);
}
Update all ur three activities with the boolean variable and the two methods.