Android - play the same sound from the beginning at each button click -
i create simple code button , sound file (mp3) , , when pressing @ button , sound file playing , if pressing several times (fast) , being playing first click , till mp3 ends...i want each click play sound beginning.
public class trafficlightsactivity extends activity implements onclicklistener { private imagebutton upbutton ; private imagebutton downbutton ; private imageview image ; private mediaplayer mp1 ; private mediaplayer mp2 ; int counter=5 ; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.traffic_lights); upbutton = (imagebutton) findviewbyid (r.id.button1) ; downbutton = (imagebutton) findviewbyid (r.id.button2) ; image = (imageview) findviewbyid(r.id.imageview1) ; mp1 = mediaplayer.create(this, r.raw.nextsound) ; mp2 = mediaplayer.create(this, r.raw.backsound) ; upbutton.setonclicklistener(this) ; downbutton.setonclicklistener(this) ; } @override public void onclick(view v) { if (upbutton == v){ if (mp1.isplaying()){ mp1.stop() ; } counter= counter+1 ; system.out.println("number of clicks is: " + counter) ; mp1.start(); } }
you can call seekto(0)
method let replay sound start without finishing sound:
if (upbutton == v){ if(mp1.isplaying()) mp1.seekto(0); mp1.start(); }
Comments
Post a Comment