Class Source

All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasElement, HasSize, HasStyle, Serializable

@NullMarked @Tag("source") public class Source extends HtmlComponent
Component representing a <source> element — one of the alternative files a Video or an Audio can play.

A media component lists the same recording in several formats, each as a source of its own, and the browser plays the first one it can decode. The type tells it which that is without downloading anything, so give it whenever you know the format.

Sources are usually created through Media.addSource(String, String) rather than directly.

See Also:
  • Constructor Details

    • Source

      public Source()
      Creates a new source without a URL.
    • Source

      public Source(String src, @Nullable String type)
      Creates a new 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
      See Also:
    • Source

      public Source(DownloadHandler downloadHandler, @Nullable String type)
      Creates a new source for the media file that the given download handler serves.

      Sets the Content-Disposition header to inline for pre-defined download handlers, created by factory methods in DownloadHandler, as well as for other AbstractDownloadHandler implementations.

      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
      See Also:
  • Method Details

    • setSrc

      public void setSrc(@Nullable String src)
      Sets the URL of the media file.
      Parameters:
      src - the URL of the media file, or null to remove the URL
    • setSrc

      public void setSrc(DownloadHandler downloadHandler)
      Sets the URL of the media file to the URL of the given DownloadHandler callback.

      Sets the Content-Disposition header to inline for pre-defined download handlers, created by factory methods in DownloadHandler, as well as for other AbstractDownloadHandler implementations.

      The handler is wrapped with DownloadHandler.allowDisabled() so that the media file is still served when the media component, or one of its ancestors, is disabled. The browser fetches it as part of rendering rather than as a user action, so blocking the request on the disabled state would leave the player empty.

      Parameters:
      downloadHandler - the download handler that serves the media file, not null
      See Also:
    • getSrc

      public @Nullable String getSrc()
      Gets the URL of the media file.
      Returns:
      the URL of the media file, or null if none has been set
      See Also:
    • setType

      public void setType(@Nullable String type)
      Sets the MIME type of the media file, such as video/mp4 or audio/mpeg. A codec parameter may be included, as in video/webm; codecs="vp9, opus".

      The browser skips a source whose type it cannot play without requesting the file, so a type that is set saves a download on every browser that plays one of the other sources.

      Parameters:
      type - the MIME type of the media file, or null to remove the type
    • getType

      public @Nullable String getType()
      Gets the MIME type of the media file.
      Returns:
      the MIME type, or null if none has been set
      See Also: