DommyCastles
DommyCastles

Reputation: 425

android null refercence on SoundPool?

I'm receiving a null reference when I try to play a sound through my Android chat app. Every time a smile appears in the protocol string, I want to play an audio sound. My sound files are in OGG format and are located in res->raw->mysound.ogg.

I have the following SoundManager class in my Android class:

package chatclient.utility;

import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManager {

private  SoundPool mSoundPool; 
private  HashMap<Integer, Integer> mSoundPoolMap; 
private  AudioManager  mAudioManager;
private  Context mContext;


public SoundManager()
{

}

public void initSounds(Context theContext) { 
     mContext = theContext;
     mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); 
     mSoundPoolMap = new HashMap<Integer, Integer>(); 
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);         
} 

public void addSound(int Index,int SoundID)
{
    mSoundPoolMap.put(1, mSoundPool.load(mContext, SoundID, 1));
}

public void playSound(int index) { 

     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
}

public void playLoopedSound(int index) { 

     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f); 
}

}

And here is my code in my main class to call it:

In my onCreate:

    mSoundManager = new chatclient.utility.SoundManager();
    mSoundManager.initSounds(getBaseContext());

    mSoundManager.addSound(1, R.raw.bigsmilesmiley ); //:D
    mSoundManager.addSound(2, R.raw.smilesmiley ); //:)
    mSoundManager.addSound(3, R.raw.tonguesmiley ); //:P
    mSoundManager.addSound(4, R.raw.confusedsmiley ); //:S
    mSoundManager.addSound(5, R.raw.ofacesmiley ); //:O
    mSoundManager.addSound(6, R.raw.sadsmiley); //:(

And then to call it:

if (pollServerResult.contains(":)")) {                
    mSoundManager.playSound(2);
} else if (pollServerResult.contains(":D)")) {    
    mSoundManager.playSound(1);
} else if (pollServerResult.contains(":P")) {         
    mSoundManager.playSound(3);
} else if (pollServerResult.contains(":S")) {       
    mSoundManager.playSound(4);
} else if (pollServerResult.contains(":O")) {       
    mSoundManager.playSound(5);
} else if (pollServerResult.contains(":(")) {       
    mSoundManager.playSound(6);
}

The only problem is I get a null reference when someone posts one of these faces.

Upvotes: 2

Views: 168

Answers (1)

Alexis C.
Alexis C.

Reputation: 93872

Change this:

public void addSound(int Index,int SoundID) {
    mSoundPoolMap.put(1, mSoundPool.load(mContext, SoundID, 1));
}

With this :

public void addSound(int Index,int SoundID) {
    mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}

Upvotes: 1

Related Questions