service - Android MediaPlayer calls onCompletion before it already finished -


guys, trying implement android foreground service play music url. mediaplayer starts , play song after while calls oncompletion() when song not finished. set onerrorlistener it's not called...

what have done:

  1. checked connection url > ok
  2. checked song duration > ok
  3. implemented partial wake lock > ?

app doesn't crash, mediaplayer call oncompletion , don't know do. here have service , activity code. in advance:

service:

public class musicservice extends service implements mediaplayer.onpreparedlistener, mediaplayer.onerrorlistener, mediaplayer.oncompletionlistener{  //filter broadcasts public final static string action_filter = "action_hhgroups";  //media actions: public final static string action_play_one = "action_play_one";  //foreground: public static boolean is_foreground = false; public final static int foreground_service = 101; public final static string action_start_foreground = "action_start_foreground"; public final static string action_stop_foreground = "action_stop_foreground";  //extras: public final static string extra_url = "extra_url";  private mediaplayer mplayer;  @override public int onstartcommand(intent intent, int flags, int startid) {     switch (intent.getaction()) {         case action_start_foreground:             log.v("servicio", "action_start_foreground");             is_foreground = true;             shownotification();             break;         case action_play_one:             log.v("servicio", "action_play_one");             initializemediaplayer(                 intent.getstringextra(musicservice.extra_url);             break;         case action_stop_foreground:             log.v("servicio", "action_stop_foreground");             is_foreground = false;             stopforeground(true);             stopself();             break;     }     return service.start_sticky; }  public void initializeplayer(string url) {     if(mplayer == null) {         mplayer = new mediaplayer();         mplayer.setwakemode(getapplicationcontext(), powermanager.partial_wake_lock);         mplayer.setonpreparedlistener(this);         mplayer.setoncompletionlistener(this);         mplayer.setonerrorlistener(this);         if(url != null && !url.isempty()) {             try {                 mplayer.setdatasource(url);                 mplayer.prepareasync();             } catch (ioexception e) {                 log.v("servicio-media", "ioexception");                 e.printstacktrace();             }         }     } else {         try {             mplayer.reset();             mplayer.setdatasource(url);             mplayer.prepareasync();         } catch (ioexception e) {             e.printstacktrace();         }     } }  @override public void ontaskremoved(intent rootintent) {     log.e("servicio", "ontaskremoved");     if(is_foreground)         stopforeground(true);     stopself(); }  @override public void oncompletion(mediaplayer mediaplayer) {     log.v("servicio", "oncompletion"); }  @override public void onprepared(mediaplayer mediaplayer) {     mediaplayer.start();     log.v("servicio-media", "onprepared. duration: " + mediaplayer.getduration()); }  @override public boolean onerror(mediaplayer mediaplayer, int what, int extra) {     log.v("servicio", "onerror");     return true; }  private void shownotification() {     intent notificationintent = new intent(this, pruebaactivity.class);     notificationintent.setaction(action_notification_main);     notificationintent.setflags(intent.flag_activity_new_task | intent.flag_activity_clear_task);      pendingintent pendingintent = pendingintent.getactivity(this, 0,             notificationintent, 0);      intent previousintent = new intent(this, musicservice.class);     previousintent.setaction(action_notification_prev);     pendingintent ppreviousintent = pendingintent.getservice(this, 0,             previousintent, 0);      intent playintent = new intent(this, musicservice.class);     playintent.setaction(action_notification_play);     pendingintent pplayintent = pendingintent.getservice(this, 0,             playintent, 0);      intent nextintent = new intent(this, musicservice.class);     nextintent.setaction(action_notification_next);     pendingintent pnextintent = pendingintent.getservice(this, 0,             nextintent, 0);      intent deleteintent = new intent(this, musicservice.class);     pendingintent pdeleteintent = pendingintent.getservice(this, 666,deleteintent,             pendingintent.flag_cancel_current);      notification notification = new notificationcompat.builder(this)             .setcontenttitle("content title")             .setticker("this ticker...")             .setcontenttext("context text")             .setsmallicon(r.drawable.ic_launcher_background)             .setcontentintent(pendingintent)             .setongoing(true)             .addaction(android.r.drawable.ic_media_previous, "prev",                     ppreviousintent)             .addaction(android.r.drawable.ic_media_play, "play",                     pplayintent)             .addaction(android.r.drawable.ic_media_next, "next",                     pnextintent).build();     startforeground(foreground_service, notification); } } 

activity:

public class pruebaactivity extends appcompatactivity implements view.onclicklistener {  string url = "http://stream.hhgroups.com/1-ey7w3hay2rtcqiutmx1sylvqnvkp1ofuko4765ui6dc-isusko%20y%20sbrv%20-%20marionetas%20(adelanto)%20-%20www.hhgroups.com/";  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_prueba);      findviewbyid(r.id.btnplaysong).setonclicklistener(this);     button btnforeground = findviewbyid(r.id.btnstartforeground);     btnforeground.setonclicklistener(this);     if(musicservice.is_foreground)         btnforeground.settext("stop foreground");     else         btnforeground.settext("start foreground"); }  @suppresslint("settexti18n") @override public void onclick(view view) {     switch(view.getid()) {         case r.id.btnstartforeground:             if(musicservice.is_foreground) {                 toast.maketext(pruebaactivity.this, "stopping foreground", toast.length_short).show();                 startservice(new intent(pruebaactivity.this, musicservice.class)                         .setaction(musicservice.action_stop_foreground));                 ((button)view).settext("start foreground");             } else {                 toast.maketext(pruebaactivity.this, "setting foreground", toast.length_short).show();                 startservice(new intent(pruebaactivity.this, musicservice.class)                         .setaction(musicservice.action_start_foreground));                 ((button)view).settext("stop foreground");             }             break;         case r.id.btnplaysong:             if(musicservice.is_foreground) {                 log.v("servicio-activity", "btnplaysong -> foreground on");                 toast.maketext(pruebaactivity.this, "playing song", toast.length_short).show();                 startservice(new intent(pruebaactivity.this, musicservice.class)                         .setaction(musicservice.action_play_one)                         .putextra(musicservice.extra_url, url));             } else {                 log.v("servicio-activity", "btnplaysong -> foreground off");                 toast.maketext(pruebaactivity.this, "setting foreground", toast.length_short).show();                 startservice(new intent(pruebaactivity.this, musicservice.class)                         .setaction(musicservice.action_start_foreground));                 toast.maketext(pruebaactivity.this, "playing song", toast.length_short).show();                 startservice(new intent(pruebaactivity.this, musicservice.class)                         .setaction(musicservice.action_play_one)                         .putextra(musicservice.extra_url, url));             }             break;     } } } 

any help?

looks server side issue. test url has "content-disposition:inline;" in response header means download not streaming.

http/1.1 200 ok date: fri, 28 jul 2017 18:20:20 gmt server: apache cache-control: max-age=2678400 content-length: 10562918 content-disposition: inline; filename="540b4a08d51bc718f1e7f30313af9526.mp3" keep-alive: timeout=10, max=800 connection: keep-alive content-type: audio/mpeg 

change test url.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -