1

I write an extra class like bellow to "control" music from different Activities. It should keep playing when I switch from Activity 1 to Activity 2. But it stops even when I call backgroundmusik.start(); again in my second Activity :

public class Music {

    public static MediaPlayer backgroundmusik;
    public static int pausedSaver;



    public static void playAudio(Context c, int id){
        backgroundmusik = MediaPlayer.create(c, id);
        backgroundmusik.setLooping(true);
        backgroundmusik.start();
    }

   public static void playFrom() {
       if(!backgroundmusik.isPlaying()) {
           backgroundmusik.seekTo(pausedSaver);
           backgroundmusik.setLooping(true);
           backgroundmusik.start();
       }
   }
    public static void playPause()
    {
        backgroundmusik.pause();
        pausedSaver = backgroundmusik.getCurrentPosition();
    }

    public static void stopAudio(){
        if(backgroundmusik.isPlaying()) {
            backgroundmusik.stop();
        }
    }
}

First Activity:

public class WelcomeActivity extends AppCompatActivity{

  Music musicPlayer;
    int previous;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_layout);

        musicPlayer.playAudio(this,R.raw.dingdong);           

            Intent intent3 = new Intent(getApplicationContext(), Main.class);
            startActivity(intent3);
            finish();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        musicPlayer.playFrom();
    }

    @Override
    protected void onStart() {
        super.onStart();
        musicPlayer.playFrom();
    }

    @Override
    protected void onResume() {
        super.onResume();
        musicPlayer.playFrom();
    }

    @Override
    protected void onPause() {
        super.onPause();
        musicPlayer.playPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        musicPlayer.playPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        musicPlayer.stopAudio();
    }

}

Second Activity:

public class Main extends AppCompatActivity{

  Music musicPlayer;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
    }
}

Thanks for your help.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
MrOrhan
  • 877
  • 6
  • 21

2 Answers2

1

Play the music from service, use foreground service.

Official Android documentation says:

that is a service that the user is actively aware of and is not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. This means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

In your case a music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.

Here is an example

Alex Kamenkov
  • 891
  • 1
  • 6
  • 16
0

Well try adding this class. Worked for me..

public class BackgroundSound extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        MediaPlayer player = MediaPlayer.create(YourActivity.this, R.raw.test_cbr); 
        player.setLooping(true); // Set looping 
        player.setVolume(100,100); 
        player.start(); 

        return null;
    }

}

Or a much better option is using a service. For more info u can visit here.

Rishav
  • 3,818
  • 1
  • 31
  • 49