Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid null refercence on SoundPool?
    text
    copied!<p>I'm receiving a <code>null</code> 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 <code>res-&gt;raw-&gt;mysound.ogg</code>.</p> <p>I have the following <code>SoundManager</code> class in my Android class:</p> <pre><code>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&lt;Integer, Integer&gt; 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&lt;Integer, Integer&gt;(); 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); } </code></pre> <p>}</p> <p>And here is my code in my main class to call it:</p> <p>In my <code>onCreate</code>:</p> <pre><code> 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); //:( </code></pre> <p>And then to call it:</p> <pre><code>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); } </code></pre> <p>The only problem is I get a <code>null</code> reference when someone posts one of these faces.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload