Class Media

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.HtmlComponent
com.vaadin.flow.component.html.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
Direct Known Subclasses:
Audio, Video

@NullMarked public abstract class Media extends com.vaadin.flow.component.HtmlComponent implements com.vaadin.flow.component.HasComponents, com.vaadin.flow.component.HasAriaLabel
Base class for the two media players the browser has built in: 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 Classes
    Modifier and Type
    Class
    Description
    static enum 
    How much of the media file the browser should fetch before playback is asked for.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a media component without any source.
    protected
    Media(Source... sources)
    Creates a media component that plays the first of the given sources the browser can decode.
  • Method Summary

    Modifier and Type
    Method
    Description
    addSource(com.vaadin.flow.server.streams.DownloadHandler downloadHandler, @Nullable String type)
    Adds a source for the media file that the given download handler serves.
    addSource(String src, @Nullable String type)
    Adds a source for the media file at the given URL.
    @Nullable Media.Preload
    Gets 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.
    boolean
    Gets whether playback starts automatically.
    boolean
    Gets whether the browser shows its own playback controls.
    boolean
    Gets whether the recording is played in a loop.
    boolean
    Gets whether the player starts muted.
    void
    setAutoplay(boolean autoplay)
    Sets whether playback starts as soon as enough of the media file has loaded, without waiting for the visitor.
    void
    setControlsVisible(boolean controlsVisible)
    Sets whether the browser shows its own playback controls — a play button, a timeline, a volume slider.
    void
    setLoop(boolean loop)
    Sets whether playback starts over from the beginning when the end of the recording is reached.
    void
    setMuted(boolean muted)
    Sets whether the recording is silent when the player is first shown.
    void
    setPreload(@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, setTitle

    Methods 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, whenAttached

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.AttachNotifier

    addAttachListener

    Methods inherited from interface com.vaadin.flow.component.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.HasAriaLabel

    getAriaLabel, getAriaLabelledBy, setAriaLabel, setAriaLabelledBy, setAriaLabelledBy

    Methods inherited from interface com.vaadin.flow.component.HasComponents

    add

    Methods inherited from interface com.vaadin.flow.component.HasComponentsOfType

    add, add, addComponentAsFirst, addComponentAtIndex, bindChildren, getChildren, getComponentAt, getComponentCount, indexOf, remove, remove, removeAll, replace

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods inherited from interface com.vaadin.flow.component.HasEnabled

    bindEnabled, isEnabled, setEnabled

    Methods 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, setWidthFull

    Methods 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

      protected Media(Source... sources)
      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

      public Source addSource(String src, @Nullable String type)
      Adds a source for the media file at the given URL.
      Parameters:
      src - the URL of the media file
      type - the MIME type of the media file, such as video/mp4, or null when 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, not null
      type - the MIME type of the media file, such as video/mp4, or null when it is not known
      Returns:
      the source that was added, so that it can be configured further
      See Also:
    • getSources

      public List<Source> 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 - true to show the playback controls, false to hide them
    • isControlsVisible

      public boolean isControlsVisible()
      Gets whether the browser shows its own playback controls.
      Returns:
      true if 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 - true to start playback automatically
    • isAutoplay

      public boolean isAutoplay()
      Gets whether playback starts automatically.
      Returns:
      true if 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 - true to play the recording in a loop
    • isLoop

      public boolean isLoop()
      Gets whether the recording is played in a loop.
      Returns:
      true if 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 - true to start the player muted
    • isMuted

      public boolean isMuted()
      Gets whether the player starts muted.
      Returns:
      true if the player starts muted
      See Also:
    • setPreload

      public void setPreload(@Nullable Media.Preload preload)
      Sets how much of the media file the browser should fetch before playback is asked for. Passing null removes the hint and leaves the decision to the browser.
      Parameters:
      preload - how much to fetch in advance, or null to let the browser decide
    • getPreload

      public @Nullable Media.Preload getPreload()
      Gets how much of the media file the browser is asked to fetch in advance.
      Returns:
      the preload hint, or null if none has been set
      See Also: