https://kotlinlang.org logo
Title
m

Matthew Laser

02/22/2022, 1:35 AM
is it possible to play sythesized (or, any actually) sounds in Compose Desktop? I was trying to play around with both the
javax.sound.midi
package, as well as the JSyn package. I was able to get the same code playing sounds from a swing
JApplet
, but haven't been able to hear anything coming out of Compose. Anyone have any ideas?
k

Kirill Grouchnikov

02/22/2022, 2:19 AM
Can you do it from a Swing's
JFrame
from a
JButton
action listener on the same machine?
y

Yan Pujante

02/22/2022, 2:21 PM
I don't know about any synthesized audio, but I use this in my compose app: I load the clip this way (in the main)
var clip = AudioSystem.getClip()

  try {
    val classLoader = Thread.currentThread().contextClassLoader
    classLoader.getResourceAsStream("sounds/Ping.wav").use { stream ->
      // implementation note: AudioSystem.getAudioInputStream needs a mark/reset stream
      // which doesn't work in the case of a jar/zip input stream => need to copy first
      val byteStream = ByteArrayInputStream(stream.readBytes())
      clip.open(AudioSystem.getAudioInputStream(byteStream))
    }
  } catch (th: Throwable) {
    // shouldn't happen
    clip = null
    th.printStackTrace()
  }
I play it this way (on button press or whatever):
clip?.framePosition = 0
        clip?.start()
m

Matthew Laser

02/22/2022, 4:39 PM
@Kirill Grouchnikov I will look into this an report back. For JSyn, I was just attempting to play sound right on startup as a sanity check, basically mimicing this example, where I tried to start up & play right at the top of the invocation to
application { ...
, as well as on button press, with no results. @Yan Pujante thank you for sharing your code, I will look into this on my system as a sanity check