android - MediaPlayer onCompleteListener doesn't trigger on background app in service -


i have implemented mediaplayer in service play files while in background. starting foregroundnotification achieve this. problem i've encountered when song finishing oncompletelistener method not triggering how should, when in app triggers.

public class playerservice extends service implements mediaplayer.onpreparedlistener,         mediaplayer.onerrorlistener, mediaplayer.oncompletionlistener,         mediaplayer.onbufferingupdatelistener{      private static final string action_play = "action.play";     private static final string action_pause = "action.pause";     private static final string action_resume = "action.resume";     private static final string action_seek = "action.seek_to";     private static final string action_next = "action.next";     private static final string action_previous = "action.previous";     private static final string action_repeat = "action.repeat";     private mediaplayer mediaplayer = null;     private string curenturl;     private boolean repeat = false;     private int currentposition;      private final broadcastreceiver receiver = new broadcastreceiver() {         @override         public void onreceive(context context, intent intent) {             log.i("test", "primesc comanda prin broadcastreceiver " + intent.getaction());             string action = intent.getaction();             if(action.equals(action_play)){                 curenturl = intent.getstringextra("musicurl");                 currentposition = intent.getintextra("musicposition", 0);                 try {                     if(mediaplayer.isplaying()) {                         mediaplayer.stop();                     }                     mediaplayer.reset();                     mediaplayer.setdatasource(intent.getstringextra("musicurl"));                     mediaplayer.prepareasync();                 } catch (ioexception e) {                     e.printstacktrace();                 }             }             else if(action.equals(action_pause)){                 if(mediaplayer.isplaying())                     mediaplayer.pause();             } else if(action.equals(action_resume)){                 if(!mediaplayer.isplaying())                     mediaplayer.start();             } else if(action.equals(action_seek)){                 mediaplayer.seekto(intent.getintextra("seekprogress", 0) * 100);                 log.i("test","dau seek la "+intent.getintextra("seekprogress", 0));             } else if(action.equals(action_next)){                 intent nextintent = new intent("action.activity.next");                 intent.putextra("nextposition", currentposition+1);                 sendbroadcast(nextintent);             } else if(action.equals(action_previous)){                 intent previousintent = new intent("action.activity.previous");                 intent.putextra("previousposition", currentposition-1);                 sendbroadcast(previousintent);             } else if(action.equals(action_repeat)) {                 repeat = intent.getbooleanextra("repeatstatus", false);             }         }     };      @override     public void oncreate() {         mediaplayer = new mediaplayer();         mediaplayer.setaudiostreamtype(audiomanager.stream_music);         mediaplayer.setonpreparedlistener(this);         mediaplayer.setoncompletionlistener(this);         mediaplayer.setonerrorlistener(this);         mediaplayer.setonbufferingupdatelistener(this);          intentfilter filter = new intentfilter();         filter.addaction(action_play);         filter.addaction(action_pause);         filter.addaction(action_next);         filter.addaction(action_resume);         filter.addaction(action_previous);         filter.addaction(action_seek);         filter.addaction(action_repeat);         registerreceiver(receiver, filter);          notification notification = new notification(r.drawable.playlisticon, gettext(r.string.ticker_text),                 system.currenttimemillis());         intent notificationintent = new intent(this, musicplayer.class);         pendingintent pendingintent = pendingintent.getactivity(this, 0, notificationintent, 0);         notification.setlatesteventinfo(this, gettext(r.string.notification_title),                 gettext(r.string.notification_message), pendingintent);         startforeground(1337, notification);          log.i("test","oncreate se apeleaza");     }      @override     public void ondestroy() {         unregisterreceiver(receiver);     }      @override     public int onstartcommand(intent intent, int flags, int startid) {          log.i("test","a pornit service-ul");          return start_sticky;     }      @override     public ibinder onbind(intent intent) {         log.i("test", "se face onbind");         return null;     }      @override     public void onprepared(mediaplayer mp) {         mp.start();          intent intent = new intent("action.activity.update_ui");         intent.putextra("duration", mp.getduration());         sendbroadcast(intent);     }      @override     public boolean onerror(mediaplayer mp, int what, int extra) {          log.i("test", "eroare incerc din nou "+what+" "+extra);          try {             if(mediaplayer.isplaying()) {                 mediaplayer.stop();             }             mediaplayer.reset();             mediaplayer.setdatasource(curenturl);             mediaplayer.prepareasync();         } catch (ioexception e) {             e.printstacktrace();         }          return true;     }      @override     public void oncompletion(mediaplayer mediaplayer) {         log.i("test", "action oncomplete");          if(repeat) {             intent intent = new intent("action.activity.repeat");             intent.putextra("currentposition", currentposition);             sendbroadcast(intent);         } else {             intent intent = new intent("action.activity.next");             intent.putextra("nextposition", currentposition+1);             sendbroadcast(intent);         }     }      @override     public void onbufferingupdate(mediaplayer mp, int percent) {          intent intent = new intent("action.activity.update_buffer");         intent.putextra("progress", percent);         intent.putextra("seekbarprogress", mp.getcurrentposition() / 100);         intent.putextra("mediacurentposition", mp.getcurrentposition());         sendbroadcast(intent);          log.i("test","current position = "+mp.getcurrentposition());      } 

as can see service implements methods of mediaplayer , initiate them in oncreate


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -