'Play sound on 1 speaker using AudioTrack
My android tablet has 4 speakers, 2 at the top and 2 at the bottom. I successfully generated a beep sound with specific frequency using AudioTrack, based from the codes I found somewhere in stackoverflow. (I forgot the link)
val duration = duration // Seconds
var freq = frequency // frequency
val sampleRate = 44100
val numSamples = (duration * sampleRate) * 2
val samples = DoubleArray(numSamples)
val buffer = ShortArray(numSamples)
for (i in 0 until numSamples) {
samples[i] = Math.sin(2 * Math.PI * i / (sampleRate / freq)) // Sine wave
buffer[i] =
(samples[i] * Short.MAX_VALUE).toInt().toShort()
}
val audioTrack = AudioTrack(
AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffer.size,
AudioTrack.MODE_STREAM
)
audioTrack.write(buffer, 0, buffer.size)
audioTrack.play()
The problem is, I want to play the beep on 1 of the speaker. I can select which speaker it will play. Is it possible? How will I able to achieve that?
PS: This is my first time posting here in stackoverflow
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
