2012-04-16 19 views
6

लगी है, मुझे वर्तमान में मेरे एंड्रॉइड गेम के साथ कोई समस्या आ रही है। आम तौर पर SoundPool.play() को कॉल करने के दौरान फ़ंक्शन को समाप्त करने के लिए लगभग 0.003 सेकंड की आवश्यकता होती है, लेकिन कभी-कभी इसमें 0.2 सेकंड लगते हैं जो मेरा गेम स्टटर बनाता है। उसकी विसंगति कहाँ से आ सकती है?एंड्रॉइड SoundPool.play() कभी-कभी

+0

हमें कुछ कोड दिखा कुछ मदद की हो सकता है। अपने गेम के बारे में कुछ भी जानने के बिना, मुझे लगता है कि यह शायद धागा से संबंधित है। – Aidanc

+0

अच्छी तरह से, यह किसी भी तरह का जटिल कोड है, आप क्या देखना चाहते हैं? –

+0

आप आवेदन में थ्रेडिंग कैसे प्रबंधित कर रहे हैं? – Aidanc

उत्तर

5

टिम के लिए धन्यवाद, खेलने के लिए थ्रेड का उपयोग करके सफलतापूर्वक समस्या को हल करने लग रहा था।

थ्रेड

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; 
    } 
}