mirror of
https://github.com/moonlight-stream/moonlight-android.git
synced 2025-07-20 11:33:06 +00:00
Use a 2 frame audio buffer if possible to reduce audio latency
This commit is contained in:
parent
9d72314b9c
commit
dfc3daabcd
@ -31,23 +31,50 @@ public class AndroidAudioRenderer implements AudioRenderer {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bufferSize = Math.max(AudioTrack.getMinBufferSize(sampleRate,
|
// We're not supposed to request less than the minimum
|
||||||
channelConfig,
|
// buffer size for our buffer, but it appears that we can
|
||||||
AudioFormat.ENCODING_PCM_16BIT),
|
// do this on many devices and it lowers audio latency.
|
||||||
FRAME_SIZE * 2);
|
// We'll try the small buffer size first and if it fails,
|
||||||
|
// use the recommended larger buffer size.
|
||||||
|
try {
|
||||||
|
// Buffer two frames of audio if possible
|
||||||
|
bufferSize = FRAME_SIZE * 2;
|
||||||
|
|
||||||
// Round to next frame
|
track = new AudioTrack(AudioManager.STREAM_MUSIC,
|
||||||
bufferSize = (((bufferSize + (FRAME_SIZE - 1)) / FRAME_SIZE) * FRAME_SIZE);
|
sampleRate,
|
||||||
|
channelConfig,
|
||||||
|
AudioFormat.ENCODING_PCM_16BIT,
|
||||||
|
bufferSize,
|
||||||
|
AudioTrack.MODE_STREAM);
|
||||||
|
track.play();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Try to release the AudioTrack if we got far enough
|
||||||
|
try {
|
||||||
|
if (track != null) {
|
||||||
|
track.release();
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
|
||||||
|
// Now try the larger buffer size
|
||||||
|
bufferSize = Math.max(AudioTrack.getMinBufferSize(sampleRate,
|
||||||
|
channelConfig,
|
||||||
|
AudioFormat.ENCODING_PCM_16BIT),
|
||||||
|
FRAME_SIZE * 2);
|
||||||
|
|
||||||
|
// Round to next frame
|
||||||
|
bufferSize = (((bufferSize + (FRAME_SIZE - 1)) / FRAME_SIZE) * FRAME_SIZE);
|
||||||
|
|
||||||
|
track = new AudioTrack(AudioManager.STREAM_MUSIC,
|
||||||
|
sampleRate,
|
||||||
|
channelConfig,
|
||||||
|
AudioFormat.ENCODING_PCM_16BIT,
|
||||||
|
bufferSize,
|
||||||
|
AudioTrack.MODE_STREAM);
|
||||||
|
track.play();
|
||||||
|
}
|
||||||
|
|
||||||
LimeLog.info("Audio track buffer size: "+bufferSize);
|
LimeLog.info("Audio track buffer size: "+bufferSize);
|
||||||
track = new AudioTrack(AudioManager.STREAM_MUSIC,
|
|
||||||
sampleRate,
|
|
||||||
channelConfig,
|
|
||||||
AudioFormat.ENCODING_PCM_16BIT,
|
|
||||||
bufferSize,
|
|
||||||
AudioTrack.MODE_STREAM);
|
|
||||||
|
|
||||||
track.play();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user