I'm trying to play a WAV file using Java Clip
val uri = Res.getUri("files/frog.wav")
val audioInputStream = AudioSystem.getAudioInputStream(File(uri))
val clip: Clip = AudioSystem.getClip()
clip.open(audioInputStream)
clip.start()
I stored frog.wav in commonMain/composeResouces/files
because according to documentation, you must put wav files in that directory and get the URI to these files with Res.getUri()
.
The route in which is located the file is inside a .jar, maybe that is the problem, but the documentation doesn't tell how to solve it.
java.io.FileNotFoundException: jar:file:\C:\Users\user\AndroidStudioProjects\Desktop\Project\composeApp\build\libs\composeApp-desktop.jar!\composeResources\romshelper.composeapp.generated.resources\files\frog.wav
What can I try next?
Solved, It was necessary to cut all the uri route until the end of the .jar, so I made a substring until !/
Also it was necessary to add buffer for mark/reset support. Finally the working code is as follows:
val uri = Res.getUri(sound.path)
val resourcePath = uri.substringAfter("!/") // getting the route inside the jar
val resourceStream: InputStream? = javaClass.getResourceAsStream("/$resourcePath")
resourceStream?.let {
val bufferedIn: InputStream = BufferedInputStream(resourceStream) // adding buffer for mark/reset support
val audioInputStream = AudioSystem.getAudioInputStream(bufferedIn)
val clip: Clip = AudioSystem.getClip()
clip.open(audioInputStream)
clip.start()
}