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.
What you will build
Section titled “What you will build”Before you start
Section titled “Before you start”- 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.
1. Add the player
Section titled “1. Add the player”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);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.
2. Show the progress
Section titled “2. Show the progress”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);Check: the blue bar grows under the video, and empties when the video loops.
3. Pause and play
Section titled “3. Pause and play”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);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.
If it does not work
Section titled “If it does not work”- 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). - 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). - The video plays without sound: the backend was registered with
audio(false), or the volume is0F. - The video starts before the user asks: add
autoplay(false)and callplay()from a click. - The picture is stretched: the default
ResourceNode.StretchType.STRETCHfills the node;CONTAINkeeps the proportions, andCOVERfills the node and crops the rest.
