I'm using SoundPool for my android app. I load about 75 one- to three-second sounds into the pool in my main activity and then reference them from other activities using the Sound method...
Firstly...DO NOT DO THIS. The Android Activity
class is not just a normal Java class and the proper (and safe) Android programming approach is that an Activity
should be self-contained. Other Activities
or app components should not have access to public
methods or data fields.
If you wan't to provide a common class to be used by multiple app components then simply create a 'helper' POJO class. If it needs to use a Context
(as SoundPool
does for example), you can simply pass the Activity
context into methods as this
. Be careful about holding a reference to the Activity
Context
however as it can cause memory leaks.
Secondly to give an easy answer to your problem...
The load(...)
methods of SoundPool
return a soundID
which is what you use to call the play(...)
method. If all you want to do is play a random sound then simply create an array of some sort and use the random number to get the soundID
from the array by index. Simple - just one line of code to access the relevant soundID
.