Playing BG Music Across Activities in Android

You can also create a service which play music using mediaplayer as below.

Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc); //OR stopService(svc); 

public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    MediaPlayer player;
    public IBinder onBind(Intent arg0) {

        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();


        player = MediaPlayer.create(this, R.raw.idil);
        player.setLooping(true); // Set looping
        player.setVolume(100,100);

    }
    public int onStartCommand(Intent intent, int flags, int startId) {


        player.start();

        return 1;
    }

    public void onStart(Intent intent, int startId) {
        // TODO



    }
    public IBinder onUnBind(Intent arg0) {
        // TODO Auto-generated method stub

        return null;
    }

    public void onStop() {

    }
    public void onPause() {

    }
    @Override
    public void onDestroy() {

        player.stop();
        player.release();
    }

    @Override
    public void onLowMemory() {

    }
}

Create a static SoundManager using either SoundPools or MediaPlayers.

Create a static flag called keepMusicGoing.

Start the music when the first activty is created.

When switching actvities set keepMusicGoing to true.

On the onStop event of your activities check if keepMusicGoing is true,if so leave the music on, then set keepMusicGoing to false.

If they press the home button the keepMusicGoing flag will be false so the music will stop when the activity loses focus.

Email me and I can send you a couple SoundManagers that I wrote one uses MediaPlayers and the other SoundPools

Chad