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

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -