Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play background music in all activities of Android app

I spend about 20 hours until now and my problem there still is . I am creating an android application that has several Activities (mainMenu , aboutUs,setting). I followed the best answered of below link and that was O.K . Music played using asyncTask isn't stopping by using cancel

When i run my app,(my code is in mainActivity ), the music begin and it does not stop when navigate to other Activities . This is GOOD . But i put a ToggleButton in my setting_activity Activity that i hope this Button starts and stops this music. NOW my question is how can i stop and/or start music again from setting_activity ?

in another solution : I create a class MusicManager and i call it`s start and stop . But this was several problems too :

  1. Music started in mainMenu_activity but only play for about 15 seconds and then stopped.
  2. I could not stop the music from another activities.At this time i play music in mainMenua_ctivity as this line codes :

    MusicManager mm = new MusicManager(this, R.raw.background_sound);
    mm.play();
    

How can i stop playing that ? 3. The music stopped when i navigate to other activities .

public class MusicManager implements OnPreparedListener {

    static MediaPlayer mPlayer;
    Context context;
    private int mySoundId;

    public MusicManager(Context ctx, int musicID) {
        context = ctx;
        mySoundId = musicID;
        mPlayer = MediaPlayer.create(context, mySoundId);
        mPlayer.setOnPreparedListener(this);
    }

    public void play() {
        mPlayer = MediaPlayer.create(context, mySoundId);

    }

    public void stop() {
        mPlayer.stop();
        mPlayer.release();
    }

    @Override
    public void onPrepared(MediaPlayer player) {
        player.start();
        mPlayer.setLooping(true);
        mPlayer.setVolume(25, 25);

    }

}

Finally i want to play a background music in all activities without stop/start music . How can i do it ?

like image 271
Mojtaba Avatar asked Dec 20 '14 11:12

Mojtaba


People also ask

Which app can play music in background?

6. YMusic. YMusic is an app that can easily play YouTube videos in background which can also be used as an offline music player for the songs on your phone. Therefore you can use YMusic just like any other music streaming apps on your mobile.

Can Android activity run in background?

However, activity can't be run unless it's on foreground. In order to achieve what you want, in onPause(), you should start a service to continue the work in activity.


1 Answers

You could put the music player in a service. This would make it independent from the Activities and you would still be able to control the playback through intents.

Here are some code example about it: https://stackoverflow.com/a/8209975/2804473 The code below is written by Synxmax here at StackOverflow, and covered in the link above:

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) {
        // TO DO
    }
    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }

    public void onStop() {

    }
    public void onPause() {

    }
    @Override
    public void onDestroy() {
        player.stop();
        player.release();
    }

    @Override
    public void onLowMemory() {

    }
}
like image 61
Simon Cedergren Malmqvist Avatar answered Nov 11 '22 21:11

Simon Cedergren Malmqvist