3つの関数(initSound、addSound、playSound)で構成されるSound.javaというクラスがあります。
public static void initSound(Context con) {
mContext = con;
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
soundPoolMap = new HashMap<Integer, Integer>();
audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
}
public static void addSound(int index, int SoundID) {
soundPoolMap.put(index, soundPool.load(mContext, SoundID, 1));
}
public static void playSound(int index) {
soundPool.play(soundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
MainGame.javaコンストラクターでinitSoundとaddSoundを呼び出しました。
Sound.initSound(getContext());
Sound.addSound(1, R.raw.machine_gun);
MainGame.javaのスレッド(ループ)内でplaySoundと呼ばれます。PlaySoundは、イベントがトリガーされたときに1秒ごとに呼び出されます(たとえば、敵が見えている場合、敵が死ぬまで軍隊(複数)が継続的に発砲(音を鳴らします)します)。
Sound.playSound(1);
問題は、サウンドが再生されるときにアプリの速度が低下することです。私がサウンドプールを使用しているのは、効果音について知る限り、サウンドプールはメディアプレーヤーよりも優れているからです。(私はメディアプレーヤーを試しましたが、ラグはさらに大きくなります。)
私が使用しているサウンドファイルは、サイズ5.44KBの.wav(unsigned 8bit PCM、1 Channel、8000hz)です。
ゲームのパフォーマンスを低下させることなくエフェクトサウンドを再生するためのより良い方法はありますか、それとも私の間違いですか?どんなアイデアや反応にも本当に感謝しています。