我正在用 libgdx 构建一个滚动射击游戏。在 Windows 中,一切运行良好,但在 android 上我得到明显的抖动,帧率从 61 fps avg 没有声音下降到 48-56 fps avg 有声音。它同时播放许多小音效,因为有很多子弹要发射,敌人要同时击中。我的声音例程:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
public class SoundFX {
static final int BGDIE = 1, BGHIT = 2, BGLASER = 3, BGSPAWN = 4, PDIE = 5, PHIT = 6, PLASER = 7, PSPAWN = 8, PAUSE = 9;
Sound S_BGDIE, S_BGHIT, S_BGLASER, S_BGSPAWN, S_PDIE, S_PHIT, S_PLASER, S_PSPAWN, S_PAUSE;
public void load()
{
S_BGDIE = Gdx.audio.newSound(Gdx.files.internal("data/sfx/badguydie.mp3"));
S_BGHIT = Gdx.audio.newSound(Gdx.files.internal("data/sfx/badguygothit.mp3"));
S_BGLASER = Gdx.audio.newSound(Gdx.files.internal("data/sfx/badguylaser.mp3"));
S_BGSPAWN = Gdx.audio.newSound(Gdx.files.internal("data/sfx/badguyspawn.mp3"));
S_PDIE = Gdx.audio.newSound(Gdx.files.internal("data/sfx/playerdie.mp3"));
S_PHIT = Gdx.audio.newSound(Gdx.files.internal("data/sfx/playergothit.mp3"));
S_PLASER = Gdx.audio.newSound(Gdx.files.internal("data/sfx/playerlaser.mp3"));
S_PSPAWN = Gdx.audio.newSound(Gdx.files.internal("data/sfx/playerspawn.mp3"));
S_PAUSE = Gdx.audio.newSound(Gdx.files.internal("data/sfx/pause.mp3"));
}
public void unload()
{
S_BGDIE.dispose();
S_BGHIT.dispose();
S_BGLASER.dispose();
S_BGSPAWN.dispose();
S_PDIE.dispose();
S_PHIT.dispose();
S_PLASER.dispose();
S_PSPAWN.dispose();
S_PAUSE.dispose();
}
public void play(int id)
{
switch(id)
{
case BGDIE:
S_BGDIE.play();
break;
case BGHIT:
S_BGHIT.play();
break;
case BGLASER:
S_BGLASER.play();
break;
case BGSPAWN:
S_BGSPAWN.play();
break;
case PDIE:
S_PDIE.play();
break;
case PHIT:
S_PHIT.play();
break;
case PLASER:
S_PLASER.play();
break;
case PSPAWN:
S_PSPAWN.play();
break;
case PAUSE:
S_PAUSE.play();
break;
default:
System.out.println("invalid sfx call");
break;
}
}
}
根据游戏中发生的情况,play 大约每秒调用 4-10 次,声音效果的持续时间不到一秒,平均每个 8kb。
这是怎么回事,我该如何解决这个问题?它使游戏看起来非常不专业,并且在困难的水平上几乎无法玩。