Reputation: 5064
I am trying to play a music in service. Problem is when I am killing the app my music stops and starts itself from beginning. How can I Keep my music running smoothly. Here is my code:
public class EngineBackground extends Service {
static MediaPlayer player;
@Override
public void onCreate() {
super.onCreate();
if(player == null){
player = MediaPlayer.create(this, R.raw.aajraate);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Upvotes: 0
Views: 84
Reputation: 40407
This is expected behavior.
Do not kill applications.
If your users do so, including by swiping out of recents, they will have to accept the momentary interruption of playback which results until Android re-creates your service and it resumes playing.
You may be able to get slightly better results by running your background service in its own process using manifest attributes, but you still cannot consider any of your code immune from being killed - for example, it may still happen if something in the foreground needs more resources.
Upvotes: 1