Skip to content

Commit

Permalink
Implement video playback demo
Browse files Browse the repository at this point in the history
  • Loading branch information
sghpjuikit committed Jan 6, 2025
1 parent 7fb928d commit 0eafa92
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gradle/project.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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/
}
Expand Down
59 changes: 59 additions & 0 deletions src/demo/main/sp/it/demo/VlcjJfxVideoPlaybackDemo.kt
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 0eafa92

Please sign in to comment.