java - Playing audio for given amount of time in Android -
i new android development.
in application, wanted audio play duration (say 20 seconds). after 20sec should not start play. used getduration() method, didn't need to.
code:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setvolumecontrolstream(audiomanager.stream_music); button button1=(button)findviewbyid(r.id.button1); button button2=(button)findviewbyid(r.id.button2); button1.setonclicklistener(this); button2.setonclicklistener(this); } public void onclick(view v) { int resid; switch (v.getid()) { case r.id.button1: resid = r.raw.a; break; case r.id.button2: resid = r.raw.b; break; default: resid = r.raw.a; break; } // release resources previous mediaplayer if (mp != null) { mp.release(); } mp = mediaplayer.create(this, resid); mp.start(); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override protected void ondestroy() { if(null!=mp){ mp.release(); } super.ondestroy(); }
try this:
mp.start(); handler handler=new handler(); handler.postdelayed(new runnable() { @override public void run() { mp.stop(); } }, 20 * 1000);
after 20 seconds, audio stopped.
Comments
Post a Comment