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
To stop the song, you can do music.stop() on Destroy. means music stops when your app closed or killed
@Override
protected void onDestroy() {
music.stop();
super.onDestroy();
}
The simplest solution is to go into your Android settings > Apps > (name of your music player), then tap "Force Stop". Confirm the operation, and the music should stop. It may not work with all music players, but it's worth a shot.
If you must have code, have you tried assigning music.pause()
? This command stops the music.
Very Simple is when you want your MediaPlayer volume off, try to set up with setVolume(int leftVolume, int RightVolume)
So whenever you want to stop your MediaPlayer's Volume off. use
music.setVolume(0,0);
Even you can pause with stop your MediaPlayer with music.pause();
And Stop mediaplayer with music.stop();
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.