Can I create my sound int objects in another Class for SoundPool (Java/Android)

11 Views Asked by At

I already have my sound effects via SoundPool working perfectly as such:

In my Sound Class:

public SoundPool getSoundPool() {

        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_GAME)
                .build();

        soundPool = new SoundPool.Builder()
                .setMaxStreams(2)
                .setAudioAttributes(audioAttributes)
                .build();

        return soundPool;
    }

And then in my MainActivity:

Sounds sounds = new Sounds();

SoundPool soundPool = sounds.getSoundPool();

int popSound = soundPool.load(getApplicationContext(), R.raw.pop_sound, 1);

soundPool.play(popSound, 1, 1, 1, 0, 1);

My issue is that now that my app has grown, I have dozens of sounds. I pass all the ints and the soundPool through various methods so that I can call them to play at the correct time. All the methods and sounds are a lot to manage, and a real pain when I need to add more.

I tried to initiate the sounds in my Sound Class as such:

int popSound = this.getSoundPool().load(getApplicationContext(), R.raw.pop_sound, 1);

However when I do so I get a crash:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

Any advice on how to resolve this? Or is this not possible? Looking for a more efficient way to manage the numerous sound effects. Thanks.

0

There are 0 best solutions below