android - Is there a faster way to switch a MediaPlayer's datasource? -


i new android development, reaching out see if there more efficient, or faster way switch mediaplayer datasource ontouch method. i'm trying create instrument play flute, audio source wont switch fast enough when press (touch) buttons.

i using playnote() method switch between audio files. advice appreciated.

public class playaggeion extends activity {  imagebutton patc1; int soundison = 1; mediaplayer mp;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_play_aggeion);     onconfigurationchanged(null);     addlistenerpatima();     mp = new mediaplayer();     playnote(r.raw.aa); }  public void addlistenerpatima() {     patc1 = (imagebutton) findviewbyid(r.id.patc1);     patc1.setontouchlistener(new view.ontouchlistener() {         @override         public boolean ontouch(view v, motionevent event) {             switch(event.getaction())             {             case motionevent.action_down:                 playnote(r.raw.bb);                 return true;              case motionevent.action_up:                 playnote(r.raw.aa);                 return true;             }             return false;         };      }); }  public void  playnote(int note){     // play note     try {         mp.reset();           mp.setdatasource(getapplicationcontext(), uri.parse("android.resource://" + getpackagename() + "/" + note));         mp.prepare();         mp.setlooping(true);         mp.start();     } catch (illegalargumentexception e) {         e.printstacktrace();     } catch (illegalstateexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     } } 

i think should use soundpool instead of mediaplayer. soundpool lets preload number of sound files , lets play them 1 after without additional delay. used in games , sound board apps, seems match needs.

more info: http://developer.android.com/reference/android/media/soundpool.html

nice tutorial: http://www.vogella.com/articles/androidmedia/article.html#tutorial_soundpool


Comments