8

I'm currently facing an issue with my android game. Normally when calling SoundPool.play() the function needs about 0.003 seconds to finish, but sometimes it takes 0.2 seconds which makes my game stutter. where could his anomaly come from?

Andreas Linden
  • 12,489
  • 7
  • 51
  • 67
  • Showing us some code could be of some help. Without knowing anything about your game I have a hunch it's probably thread related. – Aidanc Apr 16 '12 at 21:15
  • well, it's somehow complex code, what do you want to see? – Andreas Linden Apr 16 '12 at 21:15
  • How are you handling threading in the application? – Aidanc Apr 16 '12 at 21:16
  • there are no threads involved for playing sounds. the affected sound is playing inside of the onTouch event from OnTouchListener. – Andreas Linden Apr 16 '12 at 21:18
  • 2
    try to move your soundpool.play() to a separate thread, or even better create a service that handles the entire sound pool for you and method calls from the activity to trigger play events when needed. – FoamyGuy Apr 16 '12 at 21:20
  • @Tim, I tought about that, but haven't been sure if that's a good idea... – Andreas Linden Apr 16 '12 at 21:20
  • @FoamyGuy are you sure a service is best for playing sounds? Seems a bit overkill, a thread seems more appropriate from my perspective. Both would work though. – JoxTraex Dec 13 '19 at 22:25

1 Answers1

9

thanks to Tim, using a Thread for playing seemed to workaround the problem successfully.

Thread

package org.racenet.racesow.threads;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.racenet.racesow.models.SoundItem;

import android.media.SoundPool;

/**
 * Thread for playing sounds
 * 
 * @author soh#zolex
 *
 */
public class SoundThread extends Thread {

    private SoundPool soundPool;
    public BlockingQueue<SoundItem> sounds = new LinkedBlockingQueue<SoundItem>();
    public boolean stop = false;

    /**
     * Constructor
     * 
     * @param soundPool
     */
    public SoundThread(SoundPool soundPool) {

        this.soundPool = soundPool;
    }

    /**
     * Dispose a sound
     * 
     * @param soundID
     */
    public void unloadSound(int soundID) {

        this.soundPool.unload(soundID);
    }

    @Override
    /**
     * Wait for sounds to play
     */
    public void run() {         

        try {

            SoundItem item;
            while (!this.stop) {

                item = this.sounds.take();
                if (item.stop) {

                    this.stop = true;
                    break;
                }

                this.soundPool.play(item.soundID, item.volume, item.volume, 0, 0, 1);
            }

        } catch (InterruptedException e) {}
    }
}

SoundItem

package org.racenet.racesow.models;

/**
 * SoundItem will be passed to the SoundThread which
 * will handle the playing of sounds
 * 
 * @author soh#zolex
 *
 */
public class SoundItem {

    public int soundID;
    public float volume;
    public boolean stop = false;

    /**
     * Default constructor
     * 
     * @param soundID
     * @param volume
     */
    public SoundItem(int soundID, float volume) {

        this.soundID = soundID;
        this.volume = volume;
    }

    /**
     * Constructor for the item
     * which will kill the thread
     * 
     * @param stop
     */
    public SoundItem(boolean stop) {

        this.stop = stop;
    }
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Andreas Linden
  • 12,489
  • 7
  • 51
  • 67