0

I have my main Activity class, a Renderer class and my custom soundPool class (called soundMan) and I can create and access SoundPool(I.E. soundMan) objects within my Activity class without too many problems.

However, this isn't much good to me, I create all of my objects from resources within my Renderer class (GLSurfaceView.Renderer) which is running on a separate thread.

So, when I attempt to create a new soundPool (soundMan) object from my renderer class, I get the error "can't create handler inside thread that has not called looper.prepare()"

I'm sure there must be a way around this, but I can't work it out. Any help would be appreciated.

Code and 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 and use an object within my Activity class like so (in onCreate):

soundMan soundPlay = new soundMan(this);  //Create object
soundPlay.PlaySound();                    //Play the sound

However, I want to be able to do the same as the above but from my rendering thread

I know I can set my soundMan object in my Activity class to static and use it like this:

MainActivity.soundPlay.PlaySound();

But this is obviously not a good way to achieve what I'm after.

Again, examples (with code) would be appreciated.

Zippy
  • 3,826
  • 5
  • 43
  • 96

1 Answers1

1

Try this

call it inside runonUIthread

 runOnUiThread(new Runnable() {
                public void run() {
                   //do stuff

                }
            });

or

Create a Hanlder in your Activity. And use Handler, post method to update the results of UI elements

Edit: Handler example

Create a Handler like this

Handler handler; <-- declaration as a field

and in onCreate()

handler=new Handler(); 

<--initiazazation of Handler

......

and then in your thread.

handler.post(new Runnable()
{
public void run()
{
//update your UI here
}
});

//I have typed it in Editor itself, any syntax errors modifty them.

Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • Hi @Pragnani thanks, I did actually try the runOnUiThread, and initially, although I didn't get any errors, the sound wouldn't play, then the app just crashed every time I ran it for some reason - could you please post a code example of the 'Handler' method you mentioned? I did read about this but couldn't find a clear example - thanks again – Zippy May 06 '13 at 01:02
  • Thank you @Pragnani for the updated code - sorry, one other question, my Renderer class is not an inner class to my Activity class. It's in a separate file, so how would I access the handler created in my Activity class from my renderer class? Thanks again. – Zippy May 06 '13 at 01:14
  • @Zippy I have now look clearly about your code, why are you creating Object for Activity? second, SoundPool must be plain class and pass handler in the constructor and update UI there' – Pragnani May 06 '13 at 01:17
  • Hi I've created a custom class (which will later be modified and improved upon - at the moment, it's just a basic class that will play a sample), thus I'm creating an object for it to be able to use it. I'm unsure what you mean by "SoundPool must be plain class and pass handler in the constructor and update UI there" = thanks again – Zippy May 06 '13 at 01:19
  • @Zippy Do you just want to play sound in the second class? Then go for Service. And I mean you are not allowed to create object for activity, Android will take care of that with its life cycle methods. So Don't create object for Activity. – Pragnani May 06 '13 at 01:23
  • I still don't understand what you mean by 'create an object for Activity'? I'm creating an object for my custom Class soundMan. I'm not sure how else to use it other than creating an object for it? My gameloop sits in my rendering class, so yes I do want to play sounds within that thread, but I've been told to use soundPool, apparently using a service is not the way to go. – Zippy May 06 '13 at 01:27
  • @Zippy ok , what do you call it... `public class soundMan extends Activity` , When a class extend by Activity then it is called activity class or Activity, I mean this – Pragnani May 06 '13 at 01:29
  • Got you, so how can I use my custom class if I'm not supposed to make an object from it? – Zippy May 06 '13 at 01:29
  • @Zippy Don't extend it with Activity or anything, and the second solution is use service to play sound, which is the best solution http://developer.android.com/reference/android/app/Service.html – Pragnani May 06 '13 at 01:30
  • Ah I got you thanks -OK I've removed extend activity, could you please let me know how I can access my Handler from an outside class? I still can't work that out, am I supposed to make my Handler static? – Zippy May 06 '13 at 01:33