Note that there are some explanatory texts on larger screens.

plurals
  1. POPlaying sounds simultaneously Android
    primarykey
    data
    text
    <p>I am creating a game on Android and I have kind of put this problem off for a while, and am now just coming back to it. In my game I have a background beat, gun sounds, explosions... etc. and I need to be able to play them simultaneously. Right now when I call play on the SoundPool class, the currently playing sound gets interrupted and the new one starts to play. My SoundManager class is below as well as the usage. Any help would be much appreciated as this is really my first game where I have needed this many sound effects. Thanks!</p> <pre><code>public class SoundManager { private SoundPool mSoundPool; private HashMap&lt;Integer, Integer&gt; mSoundPoolMap; private AudioManager mAudioManager; private Context mContext; public SoundManager(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(index, mSoundPool.load(mContext, SoundID, 1)); } public void playSound(int index) { float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING); streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING); mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); } public void playLoopedSound(int index) { float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f); } } </code></pre> <p>... and here is an example of how I use the class.</p> <pre><code>SoundManager sm = new SoundManager(this); sm.addSound(0, R.raw.explosion); sm.playSound(0); </code></pre> <p>... So with this style I add all my sounds to the SoundPool on load and then based on user input I just want to play the sound. Does this look correct? Or should I try and go about this a different way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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