According to Android documentation and previously asked questions, SoundPool
seems to be the best option to play a sound without too much latency.
I am testing this class and currently have the following code:
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundID = mSoundPool.load(getActivity(), R.raw.son_1, 1);
I'm playing the sound inside a thread as follow:
// Play sound
System.out.println("TIC");
mSoundPool.play(soundID, 0.5f, 0.5f, 1, 0, 1f);
System.out.println("TAC");
var += 1;
mTextView.post(new Runnable() {
public void run() {
mTextView.setText(String.format("%d", var));
}
});
System.out.println("TOC");
Calling the previous code in a loop yields the following Logcat:
04-18 20:08:52.455: I/System.out(955): TIC
04-18 20:08:52.456: I/System.out(955): TAC
04-18 20:08:52.460: I/System.out(955): TOC
04-18 20:08:53.457: I/System.out(955): TIC
04-18 20:08:53.457: I/System.out(955): TAC
04-18 20:08:53.458: I/System.out(955): TOC
So everything is called pretty much at the same time. However, the sound always plays with a delay after mTextView
is updated. The delay is small but audible.
How should I proceed to lower as much as possible this delay? How does one achieve to play a sound and display something simultaneously in a game, or in a metronome for example?
I did try the solution to this question, but it didn't help.