Android MediaPlayer - Play sound twice while not finished -


i added click sound android app, when click fast on buttons, sound played once (beacause first play not finished yet). i want sound played @ each click.

i know best way implement behaviour want ?

i put instantiation of mediaplayer in oncreate() method , play in onclick()

@override protected void oncreate(bundle savedinstancestate) {     [...]     mpclic = mediaplayer.create(getapplicationcontext(),r.raw.clic); }   vbuttonleft.setonclicklistener(new view.onclicklistener() {      @override      public void onclick(view v) {         [...]                        mpclic.start();      } }); 
  • i tried stop() / prepare() before play() same thing.
  • i tried instanciate new mediaplayer @ each play, works guess not recommended (and way, method, sound in activity stop if spam button many times quickly)

thank in advance :)

try to:

vbuttonleft.setonclicklistener(new view.onclicklistener() {      @override      public void onclick(view v) {      if(mpclic.isplaying()){         mpclic.stop();         mpclic.prepare();      }         mpclic.start();      } }); 

Comments