Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

android - Issue with Soundpool and Samsung Galaxy S

I recently acquired a Samsung Galaxy S, Android 2.1 update and after running my app I found that some of the sound effects play twice concurrently. This is odd because the sound effects which exhibit this behaviour seem random - on some instances some will play twice on others they will play once as expected. This bug has not been reported on any other hardware platform for my app. I have only seen one reported incident of this on this site and the person switched to use MediaPlayer however I really want to get a remedy for this.

When the app is run it initiates the Soundpool as follows,

public static final int SOUND_EXPLOSION = 1;
public static final int SOUND_CLEAR = 2;
public static final int SOUND_CLICK = 3;
public static final int SOUND_MAGIC = 4;
public static final int SOUND_ROCKET = 5;
public static final int SOUND_MELT = 6;

private SoundPool soundPool;
private AudioManager mgr;
private HashMap<Integer, Integer> soundPoolMap;

private void initSounds()
{   
     soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);

     soundPoolMap = new HashMap<Integer, Integer>();

     soundPoolMap.put(SOUND_EXPLOSION, soundPool.load(getContext(), R.raw.explosion3, 1));
     soundPoolMap.put(SOUND_CLEAR, soundPool.load(getContext(), R.raw.pop, 1));
     soundPoolMap.put(SOUND_CLICK, soundPool.load(getContext(), R.raw.click, 1));
     soundPoolMap.put(SOUND_MAGIC, soundPool.load(getContext(), R.raw.swoosh, 1));
     soundPoolMap.put(SOUND_ROCKET, soundPool.load(getContext(), R.raw.rocket, 1));
     soundPoolMap.put(SOUND_MELT, soundPool.load(getContext(), R.raw.melt3, 1));
     mgr = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE); 
}

Sound fx are then played by calling the following sub, (PlaySound is a global toggled by the user options)

public void playSound(int sound)
{


 if (PlaySound == true)
 {
     Log.w("playSound","Playing Sound" + sound);          
     float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);   
     float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);       
     float volume = streamVolumeCurrent / streamVolumeMax; 
     soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);
 }

} 

As you can see there is a log call which I used to see how many times the sub had been called although this revealed that when the bug occurs the routine is only called once when the sound is heard twice from the device.

I have also one last sub which is called when the surface view is destroyed to tidy up.

public void ReleaseSounds()
{
  if (soundPool != null)
  {

   soundPool.release();
   soundPool = null;

  }
}

Has anyone else had this issue, if so how did you resolve it? Any help on this would be greatly appreciated,

Many thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Converting sound files to OGG in the raw folder seems to have resolved this bug - I have not seen a repeat of it since.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...