I built a cross-platform project using Kotlin Multiplatform targeting Android, Windows, and macOS. Now, I need to implement a media player that must support HLS. Because of this, I ruled out the JavaFX approach and opted for the following solution:
- On Android, I’m using Media3.
- On Windows and macOS, I’m using VLCJ.
The Android version works perfectly. However, when running with VLCJ on macOS, the video just won’t render — there’s audio, and I’ve tried controlling playback via keyboard (play, pause, seek, volume, fullscreen, exit fullscreen), and all those functions work normally.
Here is the Gradle configuration and the relevant source code under /jvmMain/.
[versions]
vlcj = "4.11.0"
[libraries]
vlcj = { module = "uk.co.caprica:vlcj", version.ref = "vlcj" }
sourceSets {
// ...
jvmMain.dependencies {
implementation(compose.desktop.currentOs)
implementation(libs.kotlinx.coroutinesSwing)
implementation(libs.vlcj)
}
}
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
fun main() = application {
Window(
onCloseRequest = ::exitApplication,
title = "Media"
) {
App()
}
}
class JVMPlatform: Platform {
override val name: String = "Java ${System.getProperty("java.version")}"
}
actual fun getPlatform(): Platform = JVMPlatform()
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.SwingPanel
import uk.co.caprica.vlcj.factory.MediaPlayerFactory
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer
import java.awt.Canvas
@Composable
actual fun Player(modifier: Modifier, url: String) {
val factory = MediaPlayerFactory()
val mediaPlayer: EmbeddedMediaPlayer = factory.mediaPlayers().newEmbeddedMediaPlayer()
SwingPanel(
factory = { Canvas() },
modifier = modifier.fillMaxSize(),
update = { canvas ->
mediaPlayer.videoSurface().set(factory.videoSurfaces().newVideoSurface(canvas))
mediaPlayer.media().play(url)
}
)
}
- Could I have misunderstood VLC’s platform support? Does supporting multiple platforms not mean it’s fully adapted for cross-platform use?
- Even if the video did render correctly, would a separate VLC player installation still be required on PC?
- This is my first KMP project, and I’m developing on macOS. Does that limit my ability to test and package the app for Windows?