2012-04-16 14 views
6

Attualmente sto affrontando un problema con il mio gioco Android. Normalmente quando si chiama SoundPool.play() la funzione ha bisogno di circa 0,003 secondi per terminare, ma a volte occorrono 0,2 secondi, il che rende il mio gioco balbettante. da dove potrebbe venire la sua anomalia?Android SoundPool.play() a volte ritarda

+0

Mostrandoci po 'di codice potrebbe essere di qualche aiuto. Senza sapere nulla del tuo gioco, ho la sensazione che sia probabilmente legato al thread. – Aidanc

+0

beh, è ​​un codice in qualche modo complesso, cosa vuoi vedere? –

+0

Come stai gestendo il threading nell'applicazione? – Aidanc

risposta

5

grazie a Tim, l'utilizzo di un thread per la riproduzione sembrava risolvere il problema con successo.

Discussione

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 soundID; 
    public volume; 
    public 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; 
    } 
} 
Problemi correlati