Audio

Struct Audio 

Source
pub struct Audio;
Expand description

The <audio> element - embeds sound content into documents.

§Purpose

The <audio> element is used to embed audio content in documents. It may contain one or more audio sources using nested <source> elements or the src attribute. The browser will choose the most suitable source. It provides built-in playback controls and APIs for programmatic control.

§Content Categories

  • Flow Content
  • Phrasing Content
  • Embedded Content
  • If with controls attribute: Interactive Content
  • Palpable Content

§Permitted Content Model

  • Zero or more <source> elements
  • Zero or more <track> elements
  • Transparent content (fallback for browsers without audio support)
  • No media elements among descendants

§Common Use Cases

  • Music players and playlists
  • Podcast players and audio articles
  • Sound effects for interactive elements
  • Audio feedback for user actions
  • Background music or ambient sound

§Key Attributes

  • src: URL of the audio file
  • controls: Show playback controls
  • autoplay: Automatically start playback (use with caution)
  • loop: Loop the audio
  • muted: Mute audio by default
  • preload: Hint for loading strategy (“none”, “metadata”, “auto”)
  • crossorigin: CORS settings for audio fetching
  • volume: Initial volume (0.0 to 1.0, set via JavaScript)

§Example

<!-- Basic audio player -->
<audio src="/audio/music.mp3" controls>
  Your browser doesn't support the audio element.
</audio>

<!-- Multiple format sources -->
<audio controls>
  <source src="/audio/podcast.opus" type="audio/opus">
  <source src="/audio/podcast.ogg" type="audio/ogg; codecs=vorbis">
  <source src="/audio/podcast.mp3" type="audio/mpeg">
  <p>Your browser doesn't support HTML5 audio. <a href="/audio/podcast.mp3">Download</a></p>
</audio>

<!-- Looping background audio -->
<audio src="/audio/ambient.mp3" loop muted autoplay>
</audio>

<!-- Audio with preloading control -->
<audio src="/audio/effect.mp3" preload="none" id="sound-effect">
</audio>

§Accessibility

  • Provide transcripts for audio-only content
  • Use captions/transcripts for important information conveyed via audio
  • Don’t use autoplay with sound, as it can be disruptive
  • Ensure playback controls are keyboard accessible
  • Consider users with hearing impairments

§WHATWG Specification

Trait Implementations§

Source§

impl HtmlElement for Audio

Source§

const TAG: &'static str = "audio"

The HTML tag name (e.g., “div”, “span”, “img”).
Source§

const VOID: bool = false

Whether this is a void element (self-closing, no children allowed).
Source§

impl CanContain<Source> for Audio

Source§

impl CanContain<Track> for Audio

Source§

impl EmbeddedContent for Audio

Source§

impl FlowContent for Audio

Source§

impl PalpableContent for Audio

Source§

impl PhrasingContent for Audio

Auto Trait Implementations§

§

impl Freeze for Audio

§

impl RefUnwindSafe for Audio

§

impl Send for Audio

§

impl Sync for Audio

§

impl Unpin for Audio

§

impl UnwindSafe for Audio

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.