java - Media player not working properly -
i making , app in press buttons play sounds.it seems work @ first after 5-6 press stops playing sounds. here code
public class mainactivity extends actionbaractivity { private imagebutton pad1, pad2, pad3, pad4, pad5, pad6, pad7, pad8, pad9, pad10, pad11, pad12; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); pad1 = (imagebutton) findviewbyid(r.id.pad1); pad2 = (imagebutton) findviewbyid(r.id.pad2); pad3 = (imagebutton) findviewbyid(r.id.pad3); pad4 = (imagebutton) findviewbyid(r.id.pad4); pad5 = (imagebutton) findviewbyid(r.id.pad5); pad6 = (imagebutton) findviewbyid(r.id.pad6); pad7 = (imagebutton) findviewbyid(r.id.pad7); pad8 = (imagebutton) findviewbyid(r.id.pad8); pad9 = (imagebutton) findviewbyid(r.id.pad9); pad10 = (imagebutton) findviewbyid(r.id.pad10); pad11 = (imagebutton) findviewbyid(r.id.pad11); pad12 = (imagebutton) findviewbyid(r.id.pad12); } @override public boolean oncreateoptionsmenu(menu menu) { return true; } public void onpad1click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p1); mp.start(); } public void onpad2click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p2); mp.start(); } public void onpad3click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p3); mp.start(); } public void onpad4click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p4); mp.start(); } public void onpad5click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p5); mp.start(); } public void onpad6click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p6); mp.start(); } public void onpad7click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p7); mp.start(); } public void onpad8click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p8); mp.start(); } public void onpad9click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p9); mp.start(); } public void onpad10click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p10); mp.start(); } public void onpad11click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p11); mp.start(); } public void onpad12click(view view) { mediaplayer mp = mediaplayer.create(this, r.raw.p12); mp.start(); } }
i dont know sounds short dont think problem. hope can solve this. thank you!
i recommend use soundpool instead of mediaplayer short playback.
add following variables:
soundpool soundpool = null; int sound1_id; int sound2_id;
add oncreate()-method (instead of mediaplayer):
soundpool = new soundpool(10, audiomanager.stream_music, 0); sound1_id = soundpool.load(this, r.raw.p1, 1); sound2_id = soundpool.load(this, r.raw.p2, 1);
and @ specific onclick():
soundpool.play(sound1_id, (float) 0.5, (float) 0.5, 1, 0, 1.f);
Comments
Post a Comment