Hello everyone. Im trying out the new <Resources> ...
# multiplatform
p
Hello everyone. Im trying out the new Resources system with compose version 1.6.11 on Kotlin 2.0.0. I have added an audio file
composeResources/files/sound.mp3
. According to the post we're supposed to use
Res.getUri("files/sound.mp3")
to get a file path to read. Im new to Compose and especially Multiplatform and Im not sure how to read the file. I couldn't find any
File
class in
commonMain
so I created an
expect/actual fun playSound(path: String)
which I tried on Android by first reading the file with:
Copy code
// path = "jar:file:/data/app/~~BCW32IAnb6EGfMOQvBQalQ==/<BUNDLE_ID>-di96yWV7A2Xl2lR01kWHBw==/base.apk!/composeResources/<PROJECT_NAME>.generated.resources/files/alarm.mp3"

actual fun playSound(path: String) {
    File(path).readBytes() // Throws java.io.FileNotFoundException
}
Any help would be highly appreciated. I just now tried with the
kotlinx-io-core
package with
SystemFileSystem.exists(Path(path))
which returns
false
p
Hi, @Philip Fryklund did you solve this? I'm trying to use Res.getUri to play a wav file, and I got
filenotfoundexception
when passing it to AudioSystem.getAudioInputStream:
Copy code
val uri = Res.getUri("files/frog.wav")
val audioInputStream = AudioSystem.getAudioInputStream(File(uri))
p
We “solved” it using a expect/actual and used a MediaPlayer on Android and AVPlayer on iOS. So we have 2 copies of the audio file and access them natively on each platform. Not ideal but solved the problem we had then. / Philip’s coworker
🙌 1
p
Hi, thanks, what do you mean with 2 copies? I just develop for compose desktop, have only a single platform, and the wav files are stored on
commonMain/composeResources/files/frog.wav
As I don't need various media player but just one, the java one I use in desktop, I don't think expect actual is necessary
the issue is the filenotfoundexception
p
Our project was iOS and Android only. So not sure about how to do it on desktop. This is how we get it on Android:
Copy code
MediaPlayer.create(context, R.raw.alarm)
The file is not in commonMain though.
So I’m afraid I can’t help 😕
p
then you solved it without using Res.getUri("") ?
p
Correct!
p
ouch
ok, thanks