title: Android SoundPool playing sound at different volumes
body: <p>I have a very strange problem that I can't figure out.  I have a soundpool initialized with a single sound (a very short 'click' sound).  In my onClick handler for my activity buttons I do a simple <code>SoundManager.playSound(1, 0);</code> which looks up the sound in a hashmap and plays the sound.  The exact same <strong>playSound</strong> call is used for every click.  It always plays the sound and hasn't erred.</p>
  
  <p>Here is the funny part though.  The volume of the click sound changes from button press to button press.  I can press it once and it sounds faint.  I hit back and then press it again and it sounds loud.  I can't find any rhyme or reason to it.  I thought maybe it was getting messed up because of load (I know that is crazy but I am at a loss here) so I even started the sound in a separate thread.  Still the same result.  I thought maybe it was due to initializing the sound manager a second time, so I added a check to ensure that doesn't happen....same result.</p>
  
  <p>I have used a sound manager class that I found several places on the web.  It is below.</p>
  
  <pre><code>public class SoundManager
  {
  
      static private SoundManager _instance;
      private static SoundPool mSoundPool;
      private static HashMap&lt;Integer, Integer&gt; mSoundPoolMap;
      private static AudioManager mAudioManager;
      private static Context mContext;
  
      public static boolean isInitialized = false;
  
      private SoundManager()
      {
      }
  
      /**
       * Requests the instance of the Sound Manager and creates it if it does not exist.
       * 
       * @return Returns the single instance of the SoundManager
       */
      static synchronized public SoundManager getInstance()
      {
          if (_instance == null) _instance = new SoundManager();
          return _instance;
      }
  
      /**
       * Initialises the storage for the sounds
       * 
       * @param theContext
       *            The Application context
       */
      @SuppressLint("UseSparseArrays")
      public static void initSounds(Context theContext)
      {
          mContext = theContext;
          mSoundPool = new SoundPool(4, AudioManager.STREAM_NOTIFICATION, 0);
          mSoundPoolMap = new HashMap&lt;Integer, Integer&gt;();
          mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
      }
  
      /**
       * Add a new Sound to the SoundPool
       * 
       * @param Index
       *            - The Sound Index for Retrieval
       * @param SoundID
       *            - The Android ID for the Sound asset.
       */
      public static void addSound(int Index, int SoundID)
      {
          mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
      }
  
      /**
       * Loads the various sound assets Currently hardcoded but could easily be changed to be flexible.
       */
      public static void loadSounds()
      {
          if (!isInitialized)
          {
              mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.click, 1));
              isInitialized = true;
          }
  
      }
  
      /**
       * Plays a Sound
       * 
       * @param index
       *            - The Index of the Sound to be played
       * @param speed
       *            - The Speed to play not, not currently used but included for compatibility
       */
      public static void playSound(int index, float speed)
      {
          float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
          streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
          mSoundPool.play(mSoundPoolMap.get(index), 1, 1, 1, 0, speed);
      }
  
      /**
       * Stop a Sound
       * 
       * @param index
       *            - index of the sound to be stopped
       */
      public static void stopSound(int index)
      {
          mSoundPool.stop(mSoundPoolMap.get(index));
      }
  
      /**
       * Deallocates the resources and Instance of SoundManager
       */
      public static void cleanup()
      {
          mSoundPool.release();
          mSoundPool = null;
          mSoundPoolMap.clear();
          mAudioManager.unloadSoundEffects();
          _instance = null;
  
      }
  
  }
  </code></pre>
  
  <p>You can see in the <strong>PlaySound</strong> method I have hard coded the volume for the left and right channel to the maximum value of 1.  This didn't help.  The volume of the sounds still fluctuates.</p>
  
  <p>I also updated the stream I am using to <code>AudioManager.STREAM_NOTIFICATION</code> instead of <code>AudioManager.STREAM_MUSIC</code> to see if it was somehow related to the stream.  It's doesn't appear to be as it exhibits the same behavior.</p>
  
  <p>I have also converted my sound file from an <code>OGG</code> to a <code>WAV</code> to see if that helped.  No change.</p>
  
  <p>It's a very quick 'click' that I am using but while listening closely it doesn't sound like it is getting cut off.  And the full file is being played.</p>
  
  <p>Could this be hardware related?  The only test device I have used is an older HTC Incredible.  Any known problems with that hardware?</p>
  
  <p>Again...the thing works but it is annoying to have the different volumes and I just can't let it go.  Has anyone else experienced this?</p>