Class Media
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier,com.vaadin.flow.component.DetachNotifier,com.vaadin.flow.component.HasAriaLabel,com.vaadin.flow.component.HasComponents,com.vaadin.flow.component.HasComponentsOfType<com.vaadin.flow.component.Component>,com.vaadin.flow.component.HasElement,com.vaadin.flow.component.HasEnabled,com.vaadin.flow.component.HasSize,com.vaadin.flow.component.HasStyle,Serializable
Video
(<video>) and Audio (<audio>).
Everything that does not depend on there being a picture — the playback
attributes and the list of sources — lives here.
A media component plays one recording, offered in as many file formats as you care to provide. Each format is a source of its own, and the browser plays the first one it can decode, which is why two or three sources are the normal case: no single format plays everywhere.
Video video = new Video();
video.addSource("/intro.webm", "video/webm");
video.addSource("/intro.mp4", "video/mp4");
video.setControlsVisible(true);
Besides the sources, the elements accept <track> elements
for subtitles and captions, and fallback content that a browser without media
support shows instead of the player. Both can be added as children too; HTML
expects them after the sources.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumHow much of the media file the browser should fetch before playback is asked for. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds a source for the media file that the given download handler serves.Adds a source for the media file at the given URL.@Nullable Media.PreloadGets how much of the media file the browser is asked to fetch in advance.Returns the sources of this media component, in the order the browser considers them.booleanGets whether playback starts automatically.booleanGets whether the browser shows its own playback controls.booleanisLoop()Gets whether the recording is played in a loop.booleanisMuted()Gets whether the player starts muted.voidsetAutoplay(boolean autoplay) Sets whether playback starts as soon as enough of the media file has loaded, without waiting for the visitor.voidsetControlsVisible(boolean controlsVisible) Sets whether the browser shows its own playback controls — a play button, a timeline, a volume slider.voidsetLoop(boolean loop) Sets whether playback starts over from the beginning when the end of the recording is reached.voidsetMuted(boolean muted) Sets whether the recording is silent when the player is first shown.voidsetPreload(@Nullable Media.Preload preload) Sets how much of the media file the browser should fetch before playback is asked for.Methods inherited from class com.vaadin.flow.component.HtmlComponent
getTitle, setTitleMethods inherited from class com.vaadin.flow.component.Component
addListener, bindVisible, findAncestor, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, getUIOrThrow, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, setVisible, whenAttachedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasAriaLabel
getAriaLabel, getAriaLabelledBy, setAriaLabel, setAriaLabelledBy, setAriaLabelledByMethods inherited from interface com.vaadin.flow.component.HasComponents
addMethods inherited from interface com.vaadin.flow.component.HasComponentsOfType
add, add, addComponentAsFirst, addComponentAtIndex, bindChildren, getChildren, getComponentAt, getComponentCount, indexOf, remove, remove, removeAll, replaceMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasEnabled
bindEnabled, isEnabled, setEnabledMethods inherited from interface com.vaadin.flow.component.HasSize
bindHeight, bindWidth, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Constructor Details
-
Media
protected Media()Creates a media component without any source. -
Media
Creates a media component that plays the first of the given sources the browser can decode.- Parameters:
sources- the same recording in one or more file formats
-
-
Method Details
-
addSource
Adds a source for the media file at the given URL.- Parameters:
src- the URL of the media filetype- the MIME type of the media file, such asvideo/mp4, ornullwhen it is not known- Returns:
- the source that was added, so that it can be configured further
- See Also:
-
addSource
public Source addSource(com.vaadin.flow.server.streams.DownloadHandler downloadHandler, @Nullable String type) Adds a source for the media file that the given download handler serves. This is the version to use for a file the application produces itself, rather than one that is served from a URL of its own.- Parameters:
downloadHandler- the download handler that serves the media file, notnulltype- the MIME type of the media file, such asvideo/mp4, ornullwhen it is not known- Returns:
- the source that was added, so that it can be configured further
- See Also:
-
getSources
Returns the sources of this media component, in the order the browser considers them. Other children, such as tracks or fallback content, are left out.- Returns:
- the sources of this media component
-
setControlsVisible
public void setControlsVisible(boolean controlsVisible) Sets whether the browser shows its own playback controls — a play button, a timeline, a volume slider. Without them, nothing on the page starts the playback, so turn them on unless the application drives the player itself.The controls are the browser's, so they do not look the same everywhere and cannot be styled through the application theme.
- Parameters:
controlsVisible-trueto show the playback controls,falseto hide them
-
isControlsVisible
public boolean isControlsVisible()Gets whether the browser shows its own playback controls.- Returns:
trueif the playback controls are shown- See Also:
-
setAutoplay
public void setAutoplay(boolean autoplay) Sets whether playback starts as soon as enough of the media file has loaded, without waiting for the visitor.Browsers block autoplay of a recording that has sound until the visitor has interacted with the site, so a player that is expected to start on its own also has to be
muted.- Parameters:
autoplay-trueto start playback automatically
-
isAutoplay
public boolean isAutoplay()Gets whether playback starts automatically.- Returns:
trueif playback starts automatically- See Also:
-
setLoop
public void setLoop(boolean loop) Sets whether playback starts over from the beginning when the end of the recording is reached.- Parameters:
loop-trueto play the recording in a loop
-
isLoop
public boolean isLoop()Gets whether the recording is played in a loop.- Returns:
trueif the recording is played in a loop- See Also:
-
setMuted
public void setMuted(boolean muted) Sets whether the recording is silent when the player is first shown.This is the initial state only. Once the visitor has unmuted the player through the browser's controls, the browser stops reading this attribute, and setting it again does not mute the player a second time.
- Parameters:
muted-trueto start the player muted
-
isMuted
public boolean isMuted()Gets whether the player starts muted.- Returns:
trueif the player starts muted- See Also:
-
setPreload
Sets how much of the media file the browser should fetch before playback is asked for. Passingnullremoves the hint and leaves the decision to the browser.- Parameters:
preload- how much to fetch in advance, ornullto let the browser decide
-
getPreload
Gets how much of the media file the browser is asked to fetch in advance.- Returns:
- the preload hint, or
nullif none has been set - See Also:
-