From 0eafa920d8e678841538c1c89fe20463ce2256f6 Mon Sep 17 00:00:00 2001 From: sghpjuikit Date: Mon, 6 Jan 2025 11:43:56 +0000 Subject: [PATCH] Implement video playback demo --- gradle/project.gradle.kts | 2 + .../sp/it/demo/VlcjJfxVideoPlaybackDemo.kt | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/demo/main/sp/it/demo/VlcjJfxVideoPlaybackDemo.kt diff --git a/gradle/project.gradle.kts b/gradle/project.gradle.kts index 50e0084a5..06832fa45 100644 --- a/gradle/project.gradle.kts +++ b/gradle/project.gradle.kts @@ -135,6 +135,8 @@ allprojects { exclude("net.java.dev.jna", "jna-jpms") exclude("net.java.dev.jna", "jna-platform-jpms") } + implementation("uk.co.caprica", "vlcj-javafx", "1.2.0") + // implementation("net.jthink", "jaudiotagger", "3.0.1") // unmaintained implementation("com.github.RouHim", "jaudiotagger", "1.2.27") // >1.2.27 has build errors and jitpack has no jar, see https://jitpack.io/com/github/RouHim/jaudiotagger/ } diff --git a/src/demo/main/sp/it/demo/VlcjJfxVideoPlaybackDemo.kt b/src/demo/main/sp/it/demo/VlcjJfxVideoPlaybackDemo.kt new file mode 100644 index 000000000..7fb431ba6 --- /dev/null +++ b/src/demo/main/sp/it/demo/VlcjJfxVideoPlaybackDemo.kt @@ -0,0 +1,59 @@ +package sp.it.demo + +import javafx.application.Application +import javafx.scene.Scene +import javafx.scene.control.Label +import javafx.scene.image.ImageView +import javafx.scene.input.TransferMode.COPY +import javafx.scene.layout.StackPane +import javafx.stage.Stage +import kotlin.text.replaceFirst +import sp.it.util.ui.size +import sp.it.util.ui.x +import uk.co.caprica.vlcj.factory.MediaPlayerFactory +import uk.co.caprica.vlcj.javafx.videosurface.ImageViewVideoSurface + +/** Demo showing how to play video in JavaFX ImageView efficiently using vlcj */ +class VlcjJfxVideoPlaybackDemo : Application() { + val factory = MediaPlayerFactory("--demux=avformat"); + val mediaPlayer = factory.mediaPlayers().newEmbeddedMediaPlayer(); + + override fun start(primaryStage: Stage) { + val imageView = ImageView().apply { + fitWidthProperty().bind(primaryStage.widthProperty()) // Bind width to stage width + fitHeightProperty().bind(primaryStage.heightProperty()) // + isPreserveRatio = true + } + + mediaPlayer.videoSurface().set(ImageViewVideoSurface(imageView)); + mediaPlayer.controls().setRepeat(true) + + primaryStage.title = "Vlcj Video Player" + primaryStage.size = 500 x 500 + primaryStage.scene = Scene(StackPane(imageView, Label("Drag & drop file to play"))) + primaryStage.show() + + // Handle drag-and-drop events + primaryStage.scene.setOnDragOver { + if (it.dragboard.hasFiles()) it.acceptTransferModes(COPY) + it.consume() + } + primaryStage.scene.setOnDragDropped { + if (it.dragboard.hasFiles()) { + val file = it.dragboard.files[0] + mediaPlayer.media().play(file.absoluteFile.toURI().toString().replaceFirst("file:/", "file:///")) + } + it.consume() + } + } + + override fun stop() { + mediaPlayer.controls().stop() + mediaPlayer.release() + } + +} + +fun main() { + Application.launch(VlcjJfxVideoPlaybackDemo::class.java) +} \ No newline at end of file