Pablo
01/27/2025, 4:44 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
*this post has been moved to compose desktop subforum, as it is more appropiated, I'm not deleting it because it haves a thread started and can help someoneWinson Chiu
01/27/2025, 4:47 PMPablo
01/27/2025, 6:27 PMPablo
01/27/2025, 6:28 PMChrimaeon
01/27/2025, 8:22 PMChrimaeon
01/27/2025, 8:28 PMPablo
01/27/2025, 9:09 PMPablo
01/27/2025, 9:10 PMPablo
01/27/2025, 9:14 PMval uri = "????"
val audioInputStream = AudioSystem.getAudioInputStream(File(uri))
val clip: Clip = AudioSystem.getClip()
clip.open(audioInputStream)
clip.start()
The problem is how to get that uri value and where to put the filesWinson Chiu
01/27/2025, 9:24 PMWinson Chiu
01/27/2025, 9:25 PMRes.readBytes
to copy the data to a file on disk, which you can then pass to whatever audio API.Chrimaeon
01/27/2025, 9:33 PMPablo
01/27/2025, 10:32 PMPablo
01/27/2025, 10:32 PMPablo
01/27/2025, 10:33 PMPablo
01/27/2025, 10:34 PMPablo
01/28/2025, 7:22 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()
}
Winson Chiu
01/28/2025, 7:24 PMPablo
01/28/2025, 7:25 PMPablo
01/28/2025, 7:26 PMPablo
01/28/2025, 7:27 PMChrimaeon
01/28/2025, 8:20 PMAudioSystem
is an JVM only class. Your code will neither work on Android nor on any other target other than JVM.
JVM Resources are, no matter if its KMP, CMP, etc., for JVM put in a directory resources
next to your java
or kotlin
folders.Chrimaeon
01/28/2025, 8:40 PMMyClass.javaClass.getResourceAsStream("/page_background.jpg")
compose desktop provides helper functions as well
https://github.com/JetBrains/compose-multiplatform/blob/aeddeba1659370ab1b553c9e3f[…]14f45/tutorials/Image_And_Icons_Manipulations/README.md?plain=1Pablo
01/30/2025, 8:58 AM