android - Can't handle with Handler -
i playing music form sd card , want seekbar getprogress every 2 sec when music playing. trying handler. dont know how use handler coretly here code:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); seekbar1 = (seekbar) findviewbyid(r.id.seekbar1); button1 = (button) findviewbyid(r.id.button1); button2 = (button) findviewbyid(r.id.button2); textview1 = (textview) findviewbyid(r.id.textview1); button1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { mediaplayer mediaplayer = new mediaplayer(); try { mediaplayer.setdatasource(environment.getexternalstoragedirectory()+"/myimages/.audio2.wav"); mediaplayer.prepare(); mediaplayer.start(); } catch (illegalargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (securityexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalstateexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } setuphandler(); } }); } private void setuphandler() { handler.removecallbacks(moveseekbarthread); handler.postdelayed(moveseekbarthread, 2000); //cal thread after millisec } private runnable moveseekbarthread = new runnable() { public void run() { if(mediaplayer.isplaying()){ int mediapos = mediaplayer.getcurrentposition(); int mediamax = mediaplayer.getduration(); seekbar1.setmax(mediamax); seekbar1.setprogress(mediapos); handler.postdelayed(this, 2000); //looping thread after second // seconds } } };
}
my app crashing 2 second after clikc button1. sure problem here handler. call handler wrong or something?
post logcat when ask app crashes. therefor in case have nullpointerexception here, think: if(mediaplayer.isplaying()) doesn't initialize mediaplayer variable.
edited:
mediaplayer mediaplayer; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); seekbar1 = (seekbar) findviewbyid(r.id.seekbar1); button1 = (button) findviewbyid(r.id.button1); button2 = (button) findviewbyid(r.id.button2); textview1 = (textview) findviewbyid(r.id.textview1); button1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { mediaplayer = new mediaplayer(); try { mediaplayer.setdatasource(environment.getexternalstoragedirectory()+"/myimages/.audio2.wav"); mediaplayer.prepare(); mediaplayer.start(); } catch (illegalargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (securityexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalstateexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } setuphandler(); } }); } private void setuphandler() { handler.removecallbacks(moveseekbarthread); handler.postdelayed(moveseekbarthread, 2000); //cal thread after millisec } private runnable moveseekbarthread = new runnable() { public void run() { if(mediaplayer.isplaying()){ int mediapos = mediaplayer.getcurrentposition(); int mediamax = mediaplayer.getduration(); seekbar1.setmax(mediamax); seekbar1.setprogress(mediapos); handler.postdelayed(this, 2000); //looping thread after second // seconds } } };
Comments
Post a Comment