Question

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 which looks like this:

    public void Sound(int s){
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    MainActivity.spool.play(s, volume, volume, 1, 0, 1f);
};

s is the integer I define in the MainActivity class, for example:

static int sound_e;

and then pass into the Sound method like this:

Sound(sound_e);

I would like to be able to define a string like this:

String letter_sound = "MainActivity.sound_" + currentLetter; 
//Example value of this string would be MainActivity.sound_a

And then pass that string into Sound as an integer. This is to avoid 26 if statements, which I did for the numbers like this:

if (randomNum == 1) {Log.v(TAG,  "Sound playing for: " + randomNum); Sound(MainActivity.sound_1);}
        else if (randomNum == 2) {Log.v(TAG,  "Sound playing for: " + randomNum); Sound(MainActivity.sound_2);}
        else if (randomNum == 3) {Log.v(TAG,  "Sound playing for: " + randomNum); Sound(MainActivity.sound_3);}
        else if (randomNum == 4) {Log.v(TAG,  "Sound playing for: " + randomNum); Sound(MainActivity.sound_4);}
        else if (randomNum == 5) {Log.v(TAG,  "Sound playing for: " + randomNum); Sound(MainActivity.sound_5);}
        else if (randomNum == 6) {Log.v(TAG,  "Sound playing for: " + randomNum); Sound(MainActivity.sound_6);}
        else if (randomNum == 7) {Log.v(TAG,  "Sound playing for: " + randomNum); Sound(MainActivity.sound_7);}
        else if (randomNum == 8) {Log.v(TAG,  "Sound playing for: " + randomNum); Sound(MainActivity.sound_8);}
        else if (randomNum == 9) {Log.v(TAG,  "Sound playing for: " + randomNum); Sound(MainActivity.sound_9);}
        else if (randomNum == 10) {Log.v(TAG,  "Sound playing for: " + randomNum); Sound(MainActivity.sound_10);}
        else Log.v(TAG, "None of the ifs are true. randomNum: " + randomNum);

I didn't see in the android documentation any way to pass string values into the SoundPool play, just integers. Thanks for helping me figure out what I'm missing!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top