6
我目前正面临着我的Android游戏的一个问题。通常当调用SoundPool.play()函数需要大约0.003秒完成,但有时需要0.2秒,这使我的游戏口吃。他的异常来自哪里?Android SoundPool.play()有时会延迟
我目前正面临着我的Android游戏的一个问题。通常当调用SoundPool.play()函数需要大约0.003秒完成,但有时需要0.2秒,这使我的游戏口吃。他的异常来自哪里?Android SoundPool.play()有时会延迟
感谢蒂姆,使用线程播放似乎成功解决了问题。
主题
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;
}
}
我们展示一些代码,可能有一定的帮助。不知道你的游戏有什么我有预感它可能是线程相关的。 – Aidanc 2012-04-16 21:15:13
好吧,它有点复杂的代码,你想看什么? – 2012-04-16 21:15:56
你如何处理应用程序中的线程? – Aidanc 2012-04-16 21:16:39