java - Mediaplayer stopped playing my sound - Android studio -


so trying onclick buttons... whenever user click butto go onclickbutton , , play sound .. button1 play mp1 button 2 play sound2 .... work fine when clicking lot on buttons after while sound stopped played .. code :

public void onclick(view v) {       final mediaplayer mp1 = mediaplayer.create(this, r.raw.sound1);     final mediaplayer mp2 = mediaplayer.create(this, r.raw.sound2);     final mediaplayer mp3 = mediaplayer.create(this, r.raw.sound3);     final mediaplayer mp4 = mediaplayer.create(this, r.raw.sound4);      textview mytv = (textview) findviewbyid(r.id.tvmytimer);     int id = v.getid();     if (id == r.id.btcomp1 || id==r.id.btuser1)     {         mytv.settext("button 1");         mp1.start();      }     if (id == r.id.btcomp2 || id==r.id.btuser2)     {         mytv.settext("button 2");         mp2.start();      }     if (id == r.id.btcomp3 || id==r.id.btuser3)     {         mytv.settext("button 3");          mp3.start();      }     if (id == r.id.btcomp4 || id==r.id.btuser4)     {         mytv.settext("button 4");          mp4.start();      }   } 

i allso disable casual button sound because wanted sound sounds ..

(disable setsoundeffectsenabled(false)  

first of all, don't create new mediaplayer in every onclick. that's unnecessary. create them once when activity starts.

second, doesn't you're doing mediaplayer except call start method. mediaplayer has rather complicated lifecycle need observe. see javadoc understand have understand make work correctly.

if want trigger sounds play in response buttons, should instead using soundpool manage , play sounds. prefer on mediaplayer you're trying do. it's easier work with.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -