android - How to access soundPool from a thread other than main/UI? -


i have main activity class, renderer class , custom soundpool class (called soundman) , can create , access soundpool(i.e. soundman) objects within activity class without many problems.

however, isn't me, create of objects resources within renderer class (glsurfaceview.renderer) running on separate thread.

so, when attempt create new soundpool (soundman) object renderer class, error "can't create handler inside thread has not called looper.prepare()"

i'm sure there must way around this, can't work out. appreciated.

code , examples follow

my custom soundpool class

public class soundman extends activity {  //simple soundpool class  private soundpool soundpool; private int soundid;  soundman(context mycontext){     soundpool = new soundpool(3, audiomanager.stream_music, 0);     soundid = soundpool.load(mycontext, r.raw.matches, 1);        }  public void playsound(){      soundpool.play(soundid, 0.9f, 0.9f, 1, 0, 0);   }  } 

i can create , use object within activity class (in oncreate):

soundman soundplay = new soundman(this);  //create object soundplay.playsound();                    //play sound 

however, want able same above rendering thread

i know can set soundman object in activity class static , use this:

mainactivity.soundplay.playsound(); 

but not way achieve i'm after.

again, examples (with code) appreciated.

try this

call inside runonuithread

 runonuithread(new runnable() {                 public void run() {                    //do stuff                  }             }); 

or

create hanlder in activity. , use handler, post method update results of ui elements

edit: handler example

create handler this

handler handler; <-- declaration field

and in oncreate()

handler=new handler();  

<--initiazazation of handler

......

and in thread.

handler.post(new runnable() { public void run() { //update ui here } }); 

//i have typed in editor itself, syntax errors modifty them.


Comments