Skip to content

Show a video

A video is a resource that a player node draws and plays, with its sound. This guide shows a looping video, a bar that follows its progress, and a button that pauses and plays it.

The bar and the label of the button follow the video on every frame; the capture loops a few seconds of it, so the bar jumps back when it starts again. Video: an excerpt of Charge © Blender Foundation, cropped to 16:9, CC BY 4.0.
  • Resources, the video formats JOID reads and the player node: Images and media.
  • Values that follow another value on every frame: Signals.
  • A backend with audio, the default: Choosing a backend.
  • A video file in the resources of your application, here src/main/resources/videos/intro.mp4.

Start from the ResourcePlayerNode of Play animated images and videos, here with the video, its proportions kept, in a loop, with the sound at 80 %. The variable player keeps the node for the next steps:

final ResourcePlayerNode player = ResourcePlayerNode
.create(560, 200, 800, 450)
.resource(Resource.of("/videos/intro.mp4"))
.stretch(StretchType.CONTAIN)
.loop(true)
.volume(0.8F)
.attach(this);

Full file: VideoGuideUI.java

Until the FFmpeg natives of the first video are ready, the node shows its skeleton (Play videos on every machine).

Check: run the UI: the video plays in the center of the window, with its sound, and starts again when it ends.

getProgress() returns how much of the video has played, from 0 to 1. A width given as a lambda reads it on every frame and turns it into a bar:

RectNode
.create(560, 670, 800, 8)
.color(VideoGuideUI.ELEMENT)
.body(bar -> {
RectNode
.create(0, 0, 0, 8)
.color(VideoGuideUI.ACCENT)
.width(() -> 800D * player.getProgress())
.attach(bar);
})
.attach(this);

Full file: VideoGuideUI.java

Check: the blue bar grows under the video, and empties when the video loops.

toggle() pauses or plays the video, and the label reads isPlaying() on every frame, so it always names the next action:

RectNode
.create(860, 710, 200, 70)
.color(VideoGuideUI.ELEMENT)
.label(() -> Text.create(player.isPlaying() ? "Pause" : "Play", info))
.onClick(event -> player.toggle())
.attach(this);

Full file: VideoGuideUI.java

Check: click the button. The video and its sound stop, the bar stops growing and the label reads Play; a second click plays it again.

Images and media lists the other controls of the player, such as seek and restart, and its listeners.

  1. A magenta and black checkerboard shows instead of the video, or nothing with the prod jar: the file cannot be read, and the dev jar prints [JOID] The resource … cannot be read: … with the cause. Check that the file is in the resources, under that path from their root (a leading / changes nothing).
  2. The error output prints [JOID] The FFmpeg natives of … cannot be downloaded from …: the machine has no access to the repository, and every video fails; put the natives on the classpath (Play videos without network access).
  3. The video plays without sound: the backend was registered with audio(false), or the volume is 0F.
  4. The video starts before the user asks: add autoplay(false) and call play() from a click.
  5. The picture is stretched: the default ResourceNode.StretchType.STRETCH fills the node; CONTAIN keeps the proportions, and COVER fills the node and crops the rest.

Report a problem with this page