I have a music service that plays music in the background of the app. I want the music to keep playing through all activities of the application but to stop when the app is running in the background (ie when the user goes to another app or presses the home button without removing the app from the running applications)
This is my MusicService code:
public class MusicService extends Service {
public static MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
player= MediaPlayer.create(this,R.raw.music1);
player.start();
player.setLooping(true);
return super.onStartCommand(intent,flags,startId);
}
}
and this is the part of my manifest related to the music service:
<service android:name=".MusicService" android:stopWithTask="true" />
Edit: If anyone knows how to play background music without service also that would be okay as long as the music plays during the whole time the app is open and closes when the home button is pressed.