views:

54

answers:

1

I have a custom class called "Sound" with SoundPool, I want to implement the loading complete listener so that my activity can play an intro sound and display the "start" button once loading is complete.

How would I go about implementing the listener and then testing for the complete status from my activity to make sure everything is loaded then go on to do the above.

A small example would be appreciated.

MyActivity creates an instance of my class "Sound" so that it can call various sound methods from it.

Sound mySound = new Sound();

Most of these are not a problem, because by the time they are called, the loading has completed, however, I need for MyActivity to be able to check if loading has completed before calling

mySound.playIntro(); 

(for example).

Maybe the OnLoadCompleteListener isn't the best solution, I'm still learning, so I'm open to ideas, this is just the way I guessed it should be done.

Thanks

+1  A: 

It would look somthing like this:

SoundPool.setOnLoadCompleteListner(new OnLoadCompleteListner(){

@override
onLoadComplete(SoundPool soundPool, int sampleId, int status)
{
// show button and play intro sound here
}});

You should read the android guide for developers. And more specificly for this problem: SoundPool

Jason
Thanks, I managed to get this far, however, as I mentioned, the stuff for showing the button and playing the sound are in the Activity, this class is a seperate file. I need to create a method here that will return the status to the main activity and can be repeatedly checked until it returns success. before playing the sound and showing the button. If you can suggest a better way I'm open to ideas. The idea is to keep the sound class independent and able to be called to check the status when necessary. Thanks
Hamid
@Hamid: " the stuff for showing the button and playing the sound are in the Activity, this class is a seperate file" -- then either don't put it in a separate file or supply an instance of the Activity to the listener object.
CommonsWare
I'm guessing the second option is the most appropriate here, how would I go about doing that?
Hamid
Have an Activity (or Context) Variable in you OnCompleteListner and pass it in the constructor ( new MyOnCompleteListner(this)) from the activity. however if all you want to do is play a sound and show a button I recommend you use an anonymous class implementation (the code I showed above inside you activity).
Jason
The "Sound" class holds all the sounds for my app. All I want it to be able to know when everything has finished loading from my Activity so that I can start using sounds, at the moment my activity doesn't know when sounds have finished loading and will try to play them even when not ready. Essentially my activity "MyActivity" creates an instance of "Sound" class. MyActivity calls methods of the "Sound" class to play sounds like mySound.playIntro(); (for example) but if the sound isn't ready it just returns an error. Hope this makes sense.
Hamid
Updated the question to be more clear.
Hamid
pass an instance of your activity to your Sound class and use that as your context ( you can also use functions in the Activity class).
Jason