android - MediaPlayer force close after consecutive clicks -


i've made button , when click it, gives away short sound(max 1 second-sound). after i've clicked button 20 times in row force close.. code is:

final button button = (button) findviewbyid(r.id.button); button.setonclicklistener(new onclicklistener() {     public void onclick(view v) {         // perform action on clicks         mediaplayer mp = mediaplayer.create(getbasecontext(), r.raw.buzzer);         mp.start();         } }); 

i've tried mp.stop(); sound stops after have been played half of time...

one more thing, know how "prepare" sound when click? because sound gets delayed milliseconds first time press button.

create mediaplayer member variable , initialize in oncreate() same way doing in listener. in listener use code:

if(mplayer.isplaying()) {     mplayer.stop(); } mplayer.start(); 

then call mplayer.release() in finish() activity. guess since none of mediaplayer instances being released, it's running out of memory use.

the official document mediaplayer incredibly descriptive , helpful: http://developer.android.com/reference/android/media/mediaplayer.html


Comments

Popular posts from this blog

How can I edit my VIM config so that Vim treats ".ejs" files the same as it currently treats my html files? -

Python __call__ special method practical example -