Skip to content

Instantly share code, notes, and snippets.

@garaei
Forked from Peppe/ExampleView.java
Created May 28, 2020 14:38
Show Gist options
  • Save garaei/a1de909aa6ad6c47592e9fa628ada07e to your computer and use it in GitHub Desktop.
Save garaei/a1de909aa6ad6c47592e9fa628ada07e to your computer and use it in GitHub Desktop.
Example on how to make an video player using the native HTML <video> in Vaadin 14.
This is an example on how to create a custom component in Vaadin 14 to make the HTML native <video> tag available from Java.
You have to add the Video.java file below to your project, where after you can do `new Video(src);` to embed videos.
I only implemented the src attribute to be dynamic, and added that the controls property should always be on.
Similarly you can add other properties like autoplay, loop, and muted.
Check https://www.html5rocks.com/en/tutorials/video/basics/ for info on what attributes are available in <video>.
If you need more example code, check from com.vaadin.flow.component.html.Image how things are done there.
Video file is placed in my spring boot app under META-INF/resources/img/video.mov.
Check https://stackoverflow.com/questions/57553973/where-should-i-place-my-vaadin-10-static-files/57553974#57553974
for where static files should go.
package com.vaadin.example.views;
import com.vaadin.example.components.Video;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
@Route("example")
public class ExampleView extends VerticalLayout {
public ExampleView() {
H1 header = new H1("My awesome video player");
Video video = new Video("img/video.mov");
add(header, video);
}
}
package com.vaadin.example.components;
import com.vaadin.flow.component.*;
@Tag("video")
public class Video extends HtmlContainer implements ClickNotifier<com.vaadin.flow.component.html.Image> {
private static final PropertyDescriptor<String, String> srcDescriptor = PropertyDescriptors
.attributeWithDefault("src", "");
public Video() {
super();
getElement().setProperty("controls", true);
}
public Video(String src) {
setSrc(src);
getElement().setProperty("controls", true);
}
public String getSrc() {
return get(srcDescriptor);
}
public void setSrc(String src) {
set(srcDescriptor, src);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment