android - seekBar.setMax(mp.getDuration()) returning 0 -


i working on mediaplayer. player working fine , music being streamed fine having trouble in setting seekbar work. assuming seekbar directly related getting correct duration through getduration , move forward.

basically, following tutorial , trying modify work if data source url rather local file.

here how setting mediaplayer object , trying getduration

uri uri = uri.parse(surl); system.out.println(uri); mediaplayer.setaudiostreamtype(audiomanager.stream_music); mediaplayer.setdatasource(this, uri);  mediaplayer.prepareasync(); mediaplayer.setonpreparedlistener(new onpreparedlistener() {        @override        public void onprepared(mediaplayer mp) {            seekbar.setmax(mp.getduration());          system.out.println("duration of file = " + mp.getduration());      }   }); initviews(mediaplayer); 

mediaplayer object initiated in oncreate. these rest of methods dealing seekbar , progress tracking.

// method set setonclicklistener , method (buttonclick()) private void initviews(mediaplayer mediaplayer) {     log.i("main", "initviews");     buttonplaystop = (button) findviewbyid(r.id.buttonplaystop);     buttonplaystop.setonclicklistener(new onclicklistener() {@override public void onclick(view v) {buttonclick();}});      seekbar = (seekbar) findviewbyid(r.id.seekbar01);            //log.i ("info", string.valueof(mediaplayer.getduration()));     seekbar.setontouchlistener(new ontouchlistener() {@override public boolean ontouch(view v, motionevent event) {         seekchange(v);         return false; }     });  }  public void startplayprogressupdater() {     seekbar.setprogress(mediaplayer.getcurrentposition());      if (mediaplayer.isplaying()) {         runnable notification = new runnable() {             public void run() {                 startplayprogressupdater();             }         };         handler.postdelayed(notification,1000);     }else{         mediaplayer.pause();         buttonplaystop.settext(getstring(r.string.play_str));         seekbar.setprogress(0);     } }   // event handler thumb moving event private void seekchange(view v){     if(mediaplayer.isplaying()){         seekbar sb = (seekbar)v;         mediaplayer.seekto(sb.getprogress());     } }  // event handler buttonclick event private void buttonclick(){     if (buttonplaystop.gettext() == getstring(r.string.play_str)) {         buttonplaystop.settext(getstring(r.string.pause_str));         try{             mediaplayer.start();             startplayprogressupdater();          }catch (illegalstateexception e) {             mediaplayer.pause();         }     }else {         buttonplaystop.settext(getstring(r.string.play_str));         mediaplayer.pause();     } } 

can please guide me in regard?


Comments