Meet gadulka, A minimalistic player library for Kotlin Multiplatform
Posted by:
-
Konstantin
Hi all. I hope you're having some spooky fun this week 🎃. On my side, I've been taking a couple of days off work which means I got time to work on some personal projects. One of these projects was an app which needed a way to play short audio files. Since I'm using Kotlin Multiplatform, this means the player functionality is platform-specific which I decided to share as an open source (BDS3) library.
Gadulka is an open source library written specifically for Kotlin Multiplatform. It offers a minimalistic implementation of an audio player, allowing apps on iOS, Android or JVM (Desktop) to play audio files.
Gadulka wraps a media player provided by each platform.
- MediaPlayer for JavaFX
- AVPlayer for iOS
- ExoPlayer for Android.
The library operates in headless mode, meaning it does not add any kind of interface or playback controls. The reason behind this is that I wanted a very minimal and streamlined implementation to support the experience of a bigger app (where I already have a design system and various components that can be used as a player UI).
The README on GitHub includes steps for getting started and available API functions. Gadulka is available on Maven Central so it's straightforward to add to an existing project using the coordinates of the latest release.
implementation("eu.iamkonstantin.kotlin:gadulka:x.x.x")
Then proceed to instantiate it and call the `play()` method by passing a URL to a WAV or MP3 file.
val player = GadulkaPlayer()
player.play("https:/...")
You can also use it with Compose:
@Composable
fun AudioPlayer(player: GadulkaPlayer = koinInject<GadulkaPlayer>()) {
val url = remember { mutableStateOf("https://...") }
Row(horizontalArrangement = Arrangement.SpaceBetween) {
Button(
onClick = {
player.play(
url.value
)
}) {
Text("Play")
}
Button(
onClick = {
player.stop()
}) {
Text("Stop")
}
}
}
This is my first Kotlin Multiplatform library, so feel free to roast me and open an Issue for any questions or challenges you may be facing while using it. Thank you!