fix(desktop): decode mp3 to pcm before opening the audio line

MP3SPI returns a stream whose format is MPEG1L3, and AudioSystem.getLine was
asked for a SourceDataLine supporting that compressed format. Java Sound has
no such line, so playback failed with "No line matching interface
SourceDataLine supporting format MPEG1L3 ...".

Convert the decoded stream to 16-bit signed PCM at the source sample rate
before opening the line, which is the format the mixer actually supports.
Add a regression test with a one-second MP3 fixture that asserts the decoded
format is PCM_SIGNED before the line is opened.
This commit is contained in:
TapTap committed 2026-11-15 08:08:58 +00:00
1 parent 8f94bb18e7
commit f8953fb200
3 files changed
+72 -1

No files matched your search

@@ -0,0 +1,34 @@
package win.taptap.musicmanager.client
import java.io.ByteArrayInputStream
import javax.sound.sampled.AudioFormat
import javax.sound.sampled.AudioSystem
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class DesktopAudioPlayerTest {
@Test
fun `mp3 is decoded to pcm before opening an audio line`() {
val bytes = checkNotNull(javaClass.getResourceAsStream("/sample.mp3")) {
"missing test fixture sample.mp3"
}.use { it.readBytes() }
val encoded = AudioSystem.getAudioInputStream(ByteArrayInputStream(bytes))
assertEquals("MPEG1L3", encoded.format.encoding.toString())
// Java Sound has no line for the compressed format; the player must
// convert to PCM_SIGNED first.
val pcm = decodeToPcm(encoded)
assertEquals(AudioFormat.Encoding.PCM_SIGNED, pcm.format.encoding)
assertEquals(44_100f, pcm.format.sampleRate)
assertEquals(16, pcm.format.sampleSizeInBits)
assertEquals(2, pcm.format.channels)
assertEquals(4, pcm.format.frameSize)
val buffer = ByteArray(4_096)
assertTrue(pcm.read(buffer) > 0)
pcm.close()
}
}
Binary file not shown.