这是我的Activity
:
public class MainActivity extends AppCompatActivity {
private SoundPool soundPool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
final int sound1 = soundPool.load(this, R.raw.whack, 1);
final int sound2 = soundPool.load(this, R.raw.miss, 1);
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playSound(sound1);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playSound(sound2);
}
});
}
private void playSound(int soundId) …
Run Code Online (Sandbox Code Playgroud) 我注意到,当在线程中使用 soundpool 玩 SFX 时,它会导致游戏中的 FPS 变慢,而在主线程中玩 SFX 时,游戏不会变慢。
为什么 soundpool 从主线程播放效果更好?
这是游戏调用:
streamID[sound] = soundPool.play(soundID[sound], getSoundEffectsVolume(), getSoundEffectsVolume(), 0, loop, 1);
Run Code Online (Sandbox Code Playgroud)