Philip Fryklund
07/03/2024, 10:10 AMcomposeResources/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:
// 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
Pablo
01/27/2025, 10:32 AMfilenotfoundexception
when passing it to AudioSystem.getAudioInputStream:
val uri = Res.getUri("files/frog.wav")
val audioInputStream = AudioSystem.getAudioInputStream(File(uri))
Per Jaakonantti
01/27/2025, 10:49 AMPablo
01/27/2025, 10:52 AMcommonMain/composeResources/files/frog.wav
Pablo
01/27/2025, 10:52 AMPablo
01/27/2025, 10:52 AMPer Jaakonantti
01/27/2025, 10:54 AMMediaPlayer.create(context, R.raw.alarm)
The file is not in commonMain though.Per Jaakonantti
01/27/2025, 10:54 AMPablo
01/27/2025, 11:16 AMPer Jaakonantti
01/27/2025, 11:16 AMPablo
01/27/2025, 11:16 AMPablo
01/27/2025, 11:16 AM