Note that there are some explanatory texts on larger screens.

plurals
  1. POSoundPool slowing down app
    primarykey
    data
    text
    <p>I have a class called Sound.java that consists of 3 functions (initSound, addSound, and playSound).</p> <pre><code>public static void initSound(Context con) { mContext = con; soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); soundPoolMap = new HashMap&lt;Integer, Integer&gt;(); audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); streamVolume = streamVolume / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); } public static void addSound(int index, int SoundID) { soundPoolMap.put(index, soundPool.load(mContext, SoundID, 1)); } public static void playSound(int index) { soundPool.play(soundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); } </code></pre> <p>I called the initSound and addSound in the MainGame.java constructor. </p> <pre><code>Sound.initSound(getContext()); Sound.addSound(1, R.raw.machine_gun); </code></pre> <p>and called playSound inside a Thread (looping) in MainGame.java. PlaySound called every second when an event is triggered (for example, when an enemy is in sight, troops (more than one) will shoot (play the sound) continuously until the enemy is dead).</p> <pre><code>Sound.playSound(1); </code></pre> <p>The problem is when the sound plays, the app is slowing down. I'm using soundpool because as far as I know for sound effects, soundpool is better than mediaplayer. (I've tried mediaplayer and the lag is even more.)</p> <p>The sound file that I use is .wav (unsigned 8bit PCM, 1 Channel, 8000hz) with size 5.44KB.</p> <p>Is there a better way to play effect sounds without slowing the game performance, or is mine wrong? I really appreciate any ideas and responses.</p>
    singulars
    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.
    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