0

I have created a service to play music through my activities. I Have 2 activities and a splashScreen. I start the service from the first acitivity and by clicking some items I go to the second activity. The music is played properly but when I put my app in background or I lock my phone or I'm in another app I still hear the sound. I cannot put stopService in onStop because if I go to my second activity, the music will stop.

Here's my service music class :

public class BackgroundMusicService extends Service {
MediaPlayer player;

public IBinder onBind(Intent arg0) {

    return null;
}

@Override
public void onCreate() {
    super.onCreate();


    player = MediaPlayer.create(this, R.raw.game_music);
    player.setLooping(true);
    player.setVolume(10, 10);
    player.start();

}

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


    player.start();

    return 1;
}



@Override
public void onDestroy() {

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

@Override
public void onLowMemory() {

}

}

and :

       public class Activity1 extends AppCompatActivity implements View.OnClickListener{
          protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_1);
                     Intent   backgSound = new Intent(Activity1.this, BackgroundMusicService.class);
        startService(backgSound);
        Button b1 = (Button) findViewById(R.id.b1);
        b1.setOnClickListener(this);


            } 

    @Override
   public void onDestroy() {
    super.onDestroy();
    Intent   backgSound = new Intent(Activity1.this, BackgroundMusicService.class);
        stopService(backgSound);
    }

        @Override
       public void onClick(View v) {
          startActivity(new Intent(this,Activity2.class);
        }

And the second activity is just a view (for test).

How could I keep the sound over these 2 activites and stop it when my app is in background or my phone is locked.

I've tried to stopService in onStop then startService in onResume, but it does not work between activities.

reg
  • 43
  • 7
  • have u call stopservice methed – Naveen Tamrakar Sep 29 '17 at 19:25
  • yup , I call stopService in onDestroy . Besides it ,"I've tried to stopService in onStop then startService in onResume, but it does not work between activities." – reg Sep 29 '17 at 19:27
  • Take a look for emample here: https://stackoverflow.com/questions/8209858/android-background-music-service/8209975#8209975 – Ofir Sep 29 '17 at 19:28
  • @reg in this code you are not calling stopservice Method in Activity – Naveen Tamrakar Sep 29 '17 at 19:31
  • ok, I just edit the code and called stopService in onDestroy as I do in my app . The problem is that I want to stop the music when I close the app. But IF I stopService in onStop() method, when I'll start my second activity , the music will stop because onStop will be called. – reg Sep 29 '17 at 19:37

2 Answers2

0

It is well known that onDestroy() is not always calling, moving your onDestroy actions to the onPause() method will work:

Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.

More info here.

You need to read more about the basics of Android.

Regards.

Brandon Zamudio
  • 2,853
  • 4
  • 18
  • 34
0

Judging by what you want you need a more fine grained control over the starting and stopping of your MediaPlayer object. An easy solution would be to add intent-filters and actions like so:

public class BackgroundMusicService extends Service {

    public static final String ACTION_START_MUSIC = "package_name.action_start_music";
    public static final String ACTION_STOP_MUSIC = "package_name.action_stop";

    private MediaPlayer player;

    public IBinder onBind(Intent arg0) {

        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        player = MediaPlayer.create(this, R.raw.game_music);
        player.setLooping(true);
        player.setVolume(10, 10);

    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent.getAction() != null){
            switch (intent.getAction()){
                case ACTION_START_MUSIC :
                    if(!player.isPlaying()){
                          player.start();
                    }
                    break;
                case ACTION_STOP_MUSIC :
                    if(player.isPlaying()) {
                         player.stop();
                    }
                    break;
                default: break;
            }
        }
        return START_STICKY;
    }



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

    @Override
    public void onLowMemory() {

    }
}

Update your manifest :

<service android:name=".BackgroundMusicService"
        android:exported="false">
        <intent-filter>
        <action android:name="package_name.action_start_music" />
        <action android:name="package_name.action_stop" />
    </intent-filter>
</service>

To use:

startService(new Intent(BackgroundMusicService.ACTION_START_MUSIC));

startService(new Intent(BackgroundMusicService.ACTION_STOP_MUSIC));

Mark
  • 9,604
  • 5
  • 36
  • 64