Class Audio<T extends Audio<T>>


public class Audio<T extends Audio<T>> extends StyledElement<T>
Represents the HTML <audio> element for embedding sound content.

The Audio class provides a Java representation of the HTML audio element, which embeds sound content in web pages. It supports various audio formats and provides controls for playback, volume, and other audio-specific features. The element can contain multiple source elements to provide format fallbacks.

All playable media attributes (autoplay, controls, crossorigin, loop, muted, preload, src) are inherited from MediaElement.

Usage Example:


 // Create an audio player with controls
 Audio audioPlayer = new Audio();
 audioPlayer.setSrc("/audio/soundtrack.mp3");
 audioPlayer.setControls(true);
 audioPlayer.setPreload("auto");

 // Create a looping background audio
 Audio bgMusic = new Audio();
 bgMusic.setSrc("/audio/background.mp3");
 bgMusic.setLoop(true);
 bgMusic.setAutoPlay(true);
 bgMusic.setMuted(false);

 // Create audio with multiple format sources
 Audio multiFormat = new Audio();
 multiFormat.setControls(true);
 Source mp3Source = new Source();
 mp3Source.setSrc("/audio/sound.mp3");
 mp3Source.setType("audio/mpeg");
 Source oggSource = new Source();
 oggSource.setSrc("/audio/sound.ogg");
 oggSource.setType("audio/ogg");
 multiFormat.addElement(mp3Source);
 multiFormat.addElement(oggSource);
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Audio

      public Audio()
      Creates a new Audio element.
  • Method Details

    • addSource

      public T addSource(Source source)
      Adds a Source element specifying an alternative media resource.

      Multiple source elements can be added to provide the same media in different formats, allowing the browser to choose the best supported format.

      Parameters:
      source - the Source element to add
      Returns:
      this element for method chaining
    • addSource

      public T addSource(String src)
      Adds a media source with the specified URL.

      Convenience method that creates a Source element with the given URL.

      Parameters:
      src - the URL of the media resource
      Returns:
      this element for method chaining
    • addTrack

      public T addTrack(Track track)
      Adds a Track element for text tracks (subtitles, captions, etc.).

      Track elements provide timed text information such as subtitles, captions, descriptions, chapters, or metadata.

      Parameters:
      track - the Track element to add
      Returns:
      this element for method chaining
    • addTrack

      public T addTrack(String src)
      Adds a text track with the specified URL.

      Convenience method that creates a Track element with the given URL.

      Parameters:
      src - the URL of the text track file (e.g., a WebVTT file)
      Returns:
      this element for method chaining
    • addFallbackContent

      public T addFallbackContent(Element fallback)
      Adds fallback content for browsers that do not support this media element.

      The fallback element is displayed in place of the media player when the browser does not support the <audio> or <video> element. Common fallback content includes a download link or descriptive text.

      Parameters:
      fallback - the element to display as fallback content
      Returns:
      this element for method chaining
    • removeAllElements

      public void removeAllElements()
      Removes all source, track, and fallback elements from this media element.
      Overrides:
      removeAllElements in class Element<T extends com.oorian.html.elements.MediaElement<T>>
    • setAutoPlay

      public T setAutoPlay(boolean autoplay)
      Sets the autoplay boolean attribute.

      Indicates that the media should begin playing automatically as soon as enough data is available. Note that many browsers block autoplay unless the media is muted.

      Parameters:
      autoplay - True to enable autoplay, false to remove.
      Returns:
      This element for method chaining.
    • setControls

      public T setControls(boolean controls)
      Sets the controls boolean attribute.

      Indicates that the browser should display playback controls (play, pause, volume, etc.) for the media.

      Parameters:
      controls - True to show controls, false to remove.
      Returns:
      This element for method chaining.
    • setCrossOrigin

      public T setCrossOrigin(CrossOrigin crossOrigin)
      Sets the cross-origin attribute using the CrossOrigin enum.
      Parameters:
      crossOrigin - The CORS mode.
      Returns:
      This element for method chaining.
    • setCrossOrigin

      public T setCrossOrigin(String crossOrigin)
      Sets the crossorigin attribute.

      Specifies how the element handles cross-origin requests. The value "anonymous" sends requests without credentials; "use-credentials" includes cookies and authentication.

      Parameters:
      crossOrigin - The cross-origin value (anonymous, use-credentials).
      Returns:
      This element for method chaining.
    • setLoop

      public T setLoop(boolean loop)
      Sets the loop boolean attribute.

      Indicates that the media should restart from the beginning upon reaching the end.

      Parameters:
      loop - True to enable looping, false to remove.
      Returns:
      This element for method chaining.
    • setMuted

      public T setMuted(boolean muted)
      Sets the muted boolean attribute.

      Indicates that the media audio output should be muted by default. This attribute is often required to enable autoplay in modern browsers.

      Parameters:
      muted - True to mute, false to remove.
      Returns:
      This element for method chaining.
    • setPreload

      public T setPreload(Preload preload)
      Sets the preload strategy using the Preload enum.
      Parameters:
      preload - The preload strategy.
      Returns:
      This element for method chaining.
    • setPreload

      public T setPreload(String preload)
      Sets the preload attribute.

      Provides a hint to the browser about how much of the media resource should be preloaded. Values include "none" (don't preload), "metadata" (preload only metadata), and "auto" (preload the entire resource).

      Parameters:
      preload - The preload hint (none, metadata, auto).
      Returns:
      This element for method chaining.
    • setSrc

      public 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.
    • isAutoPlay

      protected boolean isAutoPlay()
      Returns whether the autoplay attribute is set.
      Returns:
      true if autoplay is present, false otherwise.
    • isControls

      protected boolean isControls()
      Returns whether the controls attribute is set.
      Returns:
      true if controls is present, false otherwise.
    • getCrossOrigin

      protected String getCrossOrigin()
      Returns the crossorigin attribute value.
      Returns:
      The cross-origin value, or null if not set.
    • isLoop

      protected boolean isLoop()
      Returns whether the loop attribute is set.
      Returns:
      true if loop is present, false otherwise.
    • isMuted

      protected boolean isMuted()
      Returns whether the muted attribute is set.
      Returns:
      true if muted is present, false otherwise.
    • getPreload

      protected String getPreload()
      Returns the preload attribute value.
      Returns:
      The preload hint, or null if not set.
    • getSrc

      protected String getSrc()
      Returns the src attribute value.
      Returns:
      The resource URL, or null if not set.
    • setIntrinsicWidth

      public T setIntrinsicWidth(int pixels)
      Sets the HTML width attribute to specify intrinsic width in pixels.

      This sets the HTML attribute (e.g., <img width="300">), not the CSS width property. To set CSS width, use the inherited StyledElement.setWidth(int) method.

      Parameters:
      pixels - The width in pixels.
      Returns:
      This element for method chaining.
    • setIntrinsicWidth

      public T setIntrinsicWidth(String width)
      Sets the HTML width attribute to specify intrinsic width.

      This sets the HTML attribute, not the CSS width property. To set CSS width, use the inherited StyledElement.setWidth(String) method.

      Parameters:
      width - The width value.
      Returns:
      This element for method chaining.
    • setIntrinsicHeight

      public T setIntrinsicHeight(int pixels)
      Sets the HTML height attribute to specify intrinsic height in pixels.

      This sets the HTML attribute (e.g., <img height="200">), not the CSS height property. To set CSS height, use the inherited StyledElement.setHeight(int) method.

      Parameters:
      pixels - The height in pixels.
      Returns:
      This element for method chaining.
    • setIntrinsicHeight

      public T setIntrinsicHeight(String height)
      Sets the HTML height attribute to specify intrinsic height.

      This sets the HTML attribute, not the CSS height property. To set CSS height, use the inherited StyledElement.setHeight(String) method.

      Parameters:
      height - The height value.
      Returns:
      This element for method chaining.
    • getIntrinsicWidth

      public String getIntrinsicWidth()
      Returns the HTML width attribute value.

      Returns the intrinsic width set via the HTML attribute, not the CSS width.

      Returns:
      The width value, or null if not set.
    • getIntrinsicHeight

      public String getIntrinsicHeight()
      Returns the HTML height attribute value.

      Returns the intrinsic height set via the HTML attribute, not the CSS height.

      Returns:
      The height value, or null if not set.