Pablo
01/27/2025, 6:31 PMcommonMain/composeResouces/files
using Res.getUri()
?
I'm trying to use Res.getUri
to play a wav file, and I got filenotfoundexception
when passing it to AudioSystem.getAudioInputStream:
val uri = Res.getUri("files/frog.wav")
val audioInputStream = AudioSystem.getAudioInputStream(File(uri))
According to documentation, If you want to process multiplatform resources using other libraries included in your project, you can pass platform-specific file paths to these other APIs. To get a platform-specific path, call the Res.getUri()
function with the project path to the resource
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.Pablo
01/28/2025, 7:21 PM!/
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()
}