I’m developing an Android app that uses AudioTrack for audio playback. I have encountered an issue where the AudioTrack gets disabled due to a previous underrun error when calling releaseBuffer().
releaseBuffer() track 0x79a3234000 disabled due to previous underrun, restarting
This happens on some phones but not on others. For example, on a Xiaomi phone, the audio is heard clearly and I don’t encounter this warning, while on a Samsung phone, I do encounter this warning and the sound is choppy.
Here’s the relevant part of my code where I initialize and use the AudioTrack:
private lateinit var audioTrack: AudioTrack
private val bufferSize = 3200
private val sampleRate = 16000
private val channelConfig = AudioFormat.CHANNEL_OUT_MONO
private val audioFormat = AudioFormat.ENCODING_PCM_16BIT
...
audioTrack = AudioTrack.Builder()
.setAudioAttributes(
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
)
.setAudioFormat(
AudioFormat.Builder()
.setEncoding(audioFormat)
.setSampleRate(sampleRate)
.setChannelMask(channelConfig)
.build()
)
.setBufferSizeInBytes(bufferSize)
.setTransferMode(AudioTrack.MODE_STREAM)
.setPerformanceMode(AudioTrack.PERFORMANCE_MODE_LOW_LATENCY)
.build()
I’ve adjusted the bufferSize ranging from 300 to 6400, but this warning persists.