Class Source<T extends Source<T>>

java.lang.Object
com.oorian.html.Element<T>
com.oorian.html.elements.Source<T>

public class Source<T extends Source<T>> extends Element<T>
Represents an HTML <source> element that specifies multiple media resources.

The <source> element is used within <video>, <audio>, or <picture> elements to specify multiple media resources for different scenarios. The browser will choose the most appropriate source based on format support, screen size, and media queries. This enables responsive media and fallback support.

Features:

  • Specifies multiple media sources for video and audio
  • Enables responsive images via picture element
  • Supports media queries for conditional loading
  • Provides format fallbacks for browser compatibility
  • Self-closing tag (no closing tag required)
  • Browser automatically selects best matching source

Usage:


 // Video with multiple formats
 Video videoPlayer = new Video();
 videoPlayer.setControls(true);

 Source mp4 = new Source("/videos/movie.mp4", "video/mp4");
 videoPlayer.addElement(mp4);

 Source webm = new Source("/videos/movie.webm", "video/webm");
 videoPlayer.addElement(webm);

 Source ogg = new Source("/videos/movie.ogg", "video/ogg");
 videoPlayer.addElement(ogg);

 // Audio with fallbacks
 Audio audioPlayer = new Audio();
 audioPlayer.addElement(new Source("/audio/song.mp3", "audio/mpeg"));
 audioPlayer.addElement(new Source("/audio/song.ogg", "audio/ogg"));

 // Responsive images with picture element
 Picture responsiveImage = new Picture();

 Source mobile = new Source();
 mobile.setSrc("/images/photo-mobile.jpg");
 mobile.setMedia("(max-width: 600px)");
 responsiveImage.addElement(mobile);

 Source tablet = new Source();
 tablet.setSrc("/images/photo-tablet.jpg");
 tablet.setMedia("(max-width: 1200px)");
 responsiveImage.addElement(tablet);

 // Fallback img element
 responsiveImage.addElement(new Img("/images/photo-desktop.jpg", "Photo"));
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Source

      public Source()
      Constructs a new Source element.

      Creates an HTML <source> element. The src and type attributes should be set to specify the media resource and its format.

    • Source

      public Source(String src, String type)
      Constructs a new Source element with specified URL and media type.

      Creates an HTML <source> element with the source URL and MIME type already configured. This is a convenience constructor for quickly creating media sources.

      Parameters:
      src - the URL of the media resource
      type - the MIME type of the media resource (e.g., "video/mp4", "audio/mpeg")
  • Method Details

    • setMedia

      public final T setMedia(String mediaQuery)
      Sets the media query that this source applies to.

      Specifies the media condition under which this source should be used, enabling different media sources for different devices or viewport sizes.

      Parameters:
      mediaQuery - The media query string.
      Returns:
      This element for method chaining.
    • setSrc

      public final T setSrc(String url)
      Sets the URL of the media resource.

      If the URL starts with "/" (absolute path within the application), the servlet context path will be automatically prepended.

      Parameters:
      url - The URL of the media resource.
      Returns:
      This element for method chaining.
    • setSrcSet

      public final T setSrcSet(String srcset)
      Sets the srcset attribute for responsive image selection.

      Specifies a list of image candidates with their descriptors. Each candidate consists of a URL followed by a width descriptor (e.g., "image-480.jpg 480w") or pixel density descriptor (e.g., "image-2x.jpg 2x").

      Parameters:
      srcset - The image source set for responsive images.
      Returns:
      This element for method chaining.
    • setSizes

      public final T setSizes(String sizes)
      Sets the sizes attribute for responsive image selection.

      Specifies the display sizes of the image for different viewport conditions, used in conjunction with srcset.

      Parameters:
      sizes - The image sizes for responsive images.
      Returns:
      This element for method chaining.
    • setType

      public final T setType(String mediaType)
      Sets the MIME type of the media resource.

      Specifies the media type to help the browser determine if it can play the resource (e.g., "video/mp4", "audio/mpeg", "image/webp").

      Parameters:
      mediaType - The MIME type of the media resource.
      Returns:
      This element for method chaining.