Reputation: 17
I'm developing music player app that runs music on the background service and when I leave the app media player stops. This is my code when I leave the app media player mustn't be playing. class MusicService extends Service{
private MediaPlayer mediaPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
private void initPlayer(){
if (mediaPlayer==null){
mediaPlayer =new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(this,Settings.System.DEFAULT_RINGTONE_URI);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent!=null && intent.getAction()!=null && intent.getAction().equals("com.play")){
mediaPlayer.start();
}
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
Upvotes: 1
Views: 46
Reputation: 26
If you mean to keep playing the music in the background after leaving the app, update the manifest too:
Android background music service
as this post describes.
Upvotes: 1