1

I am trying to play music continuously in background without any button click,it should play on launching activity,and again it can be stopped by stop button.But it is not stopping as it is placed in Oncreate() ,what can be the solution for this. Please anyone tell me how to stopservice.Thankyou in advance.

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

    start = findViewById(R.id.startbutton);
    stop = findViewById(R.id.stopbutton);


    Intent i = new Intent(this, Service.class);
    this.startService(i);
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(view.getContext(), Service.class);
            stopService(i);
            Toast.makeText(MainActivity.this, "stop service called", Toast.LENGTH_SHORT).show();
        }
    });
    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(view.getContext(), Service.class);
            startService(i);
        }
    });

}

Below is my Service code

private MediaPlayer player;
public Service() {
    super();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  player= MediaPlayer.create(this,
          Settings.System.DEFAULT_RINGTONE_URI);
  player.setLooping(true);
  player.start();
  return START_STICKY;
}

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

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • can you update your question with `ESS_Service` where you start and stop the music? – Sagar Jun 29 '18 at 06:53
  • Post the Service code also... and try to use Activity context.. – SRB Bans Jun 29 '18 at 06:54
  • Possible duplicate of [Android background music service](https://stackoverflow.com/questions/8209858/android-background-music-service) – Gowthaman M Jun 29 '18 at 06:57
  • Can anyone please help me with this,I can play music when app is launched without clicking button,and can also stop it by clicklistener,but after destroying and reopening the app ,not able to stop service. – user9396014 Jun 29 '18 at 11:30

4 Answers4

1

Please Add this override method and call what ever service you need.

@Override
 public void onResume() {
  super.onResume();
  startService(new Intent(YourActivity.this, ServiceName.class));
}
Harish Reddy
  • 922
  • 10
  • 20
0

I have thought of two solutions :

1:Just don't use clicklistener . Immediately start the service when launching the activity

2: If you want to use start button , call start.performClick()

To stop the player on home screen use this method

    @Override
public void onPause() {
    super.onPause();
    player.stop();
}
Kevin Kurien
  • 812
  • 6
  • 14
0

I am returning START_NOT_STICKY in onStartCommand method in Service class instead of START_STICKY.Because of that my service was restarting every time when I reopen the app.So now I am able to stop service and my problem got solved.

0
@Override
protected void onPause() {
    super.onPause();
    this.stopService(i);
}

@Override
protected void onResume() {
    super.onResume();
    this.startService(i);
}

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    start = findViewById(R.id.startbutton);
    stop = findViewById(R.id.stopbutton);


     i = new Intent(this, MusicService.class);
    this.startService(i);
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(i!=null) {
                stopService(i);
                Toast.makeText(MainActivity.this, "stop service called", Toast.LENGTH_SHORT).show();
            }
            }
    });
}
saiyan
  • 551
  • 1
  • 4
  • 20
  • Make intent a class variable and override onPause and onResume. That should do the trick. As mentioned by Harish Reddy. – saiyan Jan 21 '22 at 06:11