media player - Android MediaPlayer : How do i show a progress dialog while buffering audio -
im trying play mp3 audio url. when having slow network, player seems not responding while buffering. need add progress dialog while buffering . possible.? if yes can me do.?
public class player extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_player); bundle b = new bundle(); b = getintent().getextras(); final string url = b.getstring("audio"); log.e("url: ", url); final mediaplayer mmediaplayer = new mediaplayer(); mmediaplayer.setaudiostreamtype(audiomanager.stream_music); try { log.e("preparing: ", url); mmediaplayer.setdatasource(url); mmediaplayer.prepareasync(); // might take long! (for buffering, etc) } catch (ioexception e) { toast.maketext(this, "mp3 not found", toast.length_short).show(); e.printstacktrace(); } //mp3 started after completion of preparing... mmediaplayer.setonpreparedlistener(new mediaplayer.onpreparedlistener() { @override public void onprepared(mediaplayer player) { int max = player.getduration(); log.e("max", string.valueof(max)); player.start(); log.e("start: ", url); } }); } }
you can show seekbar, :
// in oncreate(), add below @ last ... . . . seekbar.setmax(mmediaplayer.getduration();); mmediaplayer.setonbufferingupdatelistener(new mediaplayer.onbufferingupdatelistener() { public void onbufferingupdate(mediaplayer mp, int percent) { double ratio = percent / 100.0; bufferinglevel = (int)(mp.getduration() * ratio); seekbar.setsecondaryprogress(bufferinglevel); } });
hope helps !
Comments
Post a Comment