android - How to avoid clicking Play audio option when the audio is buffering? -
i playing mp3 audio server using below code. working fine, no issues. when clicking on play button, taking few seconds start audio, takes buffering time, in gap time if click play button again , again, app crashes. how can have condition under play button code checking whether audio has been buffered , play starting or not? can't check here if(mediaplayer.isplaying()).
private void playaudio(string url) throws exception { if( (mediaplayer == null || iscompleted) && !ispaused ) { iscompleted = false; ispaused = false; playpausebutton.setbackgroundresource(r.drawable.pauseimage); mediaplayer = new mediaplayer(); mediaplayer.setdatasource(url); mediaplayer.prepare(); mediaplayer.start(); } else if ( ispaused ) { playpausebutton.setbackgroundresource(r.drawable.pauseimage); mediaplayer.start(); ispaused = false; } else { playpausebutton.setbackgroundresource(r.drawable.playimage); ispaused = true; mediaplayer.pause(); } mediaplayer.setoncompletionlistener(new oncompletionlistener(){ @override public void oncompletion(mediaplayer mp) { // todo auto-generated method stub iscompleted = true; ispaused = false; playpausebutton.setbackgroundresource(r.drawable.playimage); }} ); } public void stopaudio(view view) { killmediaplayer(); } @override protected void ondestroy() { super.ondestroy(); killmediaplayer(); } private void killmediaplayer() { ispaused = false; iscompleted = true; playpausebutton.setbackgroundresource(r.drawable.playimage); if(mediaplayer!=null) { try { mediaplayer.release(); } catch(exception e) { e.printstacktrace(); } } }
you can block user ui progressdialog or that.
in pseudo-code:
mediaplayer.setonpreparedlistener(this); mediaplayer.prepare(); mprogressdialog = new progressdialog(this); mprogressdialog.setmessage("preparing audio"); mprogressdialog.show();
and when callback triggered can start mediaplayer:
public void onprepared(mediaplayer mp) { mprogressdialog.dismiss(); mp.start(); }
Comments
Post a Comment