Class Source<T extends Source<T>>
<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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal TSets the media query that this source applies to.final TSets the sizes attribute for responsive image selection.final TSets the URL of the media resource.final TSets the srcset attribute for responsive image selection.final TSets the MIME type of the media resource.Methods inherited from class com.oorian.html.Element
addAttribute, addAttribute, addAttribute, addAttribute, addElement, addLineBreak, addLineOfText, addLineOfText, addLineOfText, addParagraph, addParagraph, addSpacer, addText, addText, addText, addText, assignId, containsElement, containsElement, create, dispatchEvent, dispatchEvent, dispatchEvent, dispatchEvent, equals, excludeId, executeJs, executeJs, executeJs, getAccept, getAllElements, getAncestor, getAttribute, getAttributes, getComponent, getDir, getElement, getElementById, getElementCount, getElementCount, getElements, getElements, getElementsByComponentName, getElementsByTagName, getHtml, getHtml, getId, getInnerHtml, getInnerHtml, getIs, getItemId, getItemProp, getItemRef, getItemType, getLang, getNextSibling, getPage, getParent, getPart, getPrevSibling, getSlot, getTagName, getTextContent, getUrl, hasAttribute, hasElements, hidden, initialize, insertElement, isChildOf, isClosedTag, isCreated, isDescendantOf, isDescendantOf, isInitialized, isItemScope, isTranslate, onCallback, onCreated, onElementAdded, onElementRemoved, onHashChange, onHidden, onInitialized, onJsReturn, onPageLoaded, onPageUnloaded, onRefresh, onRemovedFromPage, onShown, onUpdated, onUserEvent, prewrite, recreate, refresh, refresh, registerAddition, registerListener, registerListener, registerListener, registerListener, registerListener, registerListener, registerListener, registerSubtraction, registerUpdate, removeAllElements, removeAttribute, removeAttribute, removeElement, removeElement, removeFromParent, requestCallback, requestCallback, requestCallback, requestCallback, resetId, scrollTo, scrollToBottom, scrollToTop, self, sendCommand, sendUpdate, setAccept, setComponent, setDir, setDir, setElement, setId, setIs, setItemId, setItemProp, setItemRef, setItemScope, setItemType, setLang, setOnError, setOnLoad, setPage, setParent, setPart, setSlot, setTagName, setText, setText, setText, setText, setTranslate, shown, toString, unregisterListener, update, updateAttributes
-
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
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 resourcetype- the MIME type of the media resource (e.g., "video/mp4", "audio/mpeg")
-
-
Method Details
-
setMedia
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
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
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
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
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.
-