0

I am using a Service class to play music in the background. While I am displaying the notification bar on top, there is issue in my app. When I kill the app, the music stops for about 1 second and then it starts again. I can't figure out what's the issue.

I was following this tutorial.

public class MyMusicService extends Service {

    MediaPlayer mediaPlayer;

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

    public MyMusicService() {
        super();
    }

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

        if (mediaPlayer!= null)
        {
            mediaPlayer.release();
        }
        mediaPlayer =MediaPlayer.create(this,R.raw.song);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();

         return START_STICKY;
    }

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

            mediaPlayer.stop();


    }
}
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • try calling mediaplayer.stop(); before the super.onDestroy(); – Mohamed Abdou Mar 07 '18 at 13:57
  • @MATex tried but not helping –  Mar 07 '18 at 14:00
  • Had this exact issue once.. I strongly recommend you use the default Android service for an audio app, this way the system will not kill your app... you'll have to read carefully and write a lot of code, but it will fix all your problems.. This is from a long and painful experience with music playing apps :) . https://developer.android.com/guide/topics/media-apps/audio-app/building-an-audio-app.html – HedeH Mar 07 '18 at 14:00
  • @HedShafran Thanks but i for now i am just testing my code with help of music player, i can' t find the bug –  Mar 07 '18 at 14:09
  • As you wish... This is what solved my bug at the time, and it was a really good practice as well :) – HedeH Mar 07 '18 at 14:15

3 Answers3

1

Use onTaskRemoved on your main activity and release your mediaplayer on this event.

Mag San
  • 11
  • 1
0

According to: https://developer.android.com/reference/android/app/Service.html Change

START_STICKY

to

START_NOT_STICKY

EDIT:

The issue with music stopping for a while is connected with the fact that you run app and service in the same process. And 'killing app' means stopping the whole process bound to it. What you need to do is to run your service in seperate process:

Start a service in a separate process android

muminers
  • 1,190
  • 10
  • 19
  • i also tried this but music stops when app is killed,...anyways thanks –  Mar 07 '18 at 14:17
  • Oh I tought that is exactly what you want. So what's a problem anyway? You do not want to stop the music at all? – muminers Mar 07 '18 at 14:21
  • no....the music should continuously play even if i kill the app...and its playing but when i kill the app there i pause of 1 sec before the music starts again –  Mar 07 '18 at 14:22
  • @AvadaKedavra try to start media player in onCreate instead of on start command coz when you kill your app the service restarted and on start commmand is called again – Adeel Turk Mar 07 '18 at 14:25
  • @AvadaKedavra its because your service is STICKY – Adeel Turk Mar 07 '18 at 14:27
  • applying in the onCreate has the same issue...i will try another method –  Mar 07 '18 at 14:42
0

That tutorial shows you how to build a BACKGROUND service, which can get killed by the system. To play music you should use a FOREGROUND service.

https://developer.android.com/guide/components/services.html#Foreground

ahomphophone
  • 319
  • 1
  • 8