We use cookies to ensure you get the best experience on our website. Read our privacy policy

Skip to main content

Directory

Audio Component

The Audio component provides a customizable audio player with intuitive controls, lazy loading capabilities, and comprehensive accessibility support. Built as a wrapper around the native HTML5 audio element, it offers enhanced functionality while maintaining broad compatibility.

  • Check Circle

    Playback Controls: Play/pause, seek, and volume controls

  • Check Circle

    Smart Loading: Lazy loading with intersection observer support

  • Check Circle

    Volume Management: Mute toggle and volume slider

  • Check Circle

    Keyboard Navigation: Full keyboard support for all controls

  • Check Circle

    Accessibility: ARIA-compliant with screen reader support

Create a simple audio player with default settings:

0:00 / 0:00
<Audio src="/assets/audio/example.mp3" />

The Audio component comes with several carefully designed variants to match different visual contexts using the variant prop:

0:00 / 0:00
0:00 / 0:00
0:00 / 0:00
0:00 / 0:00

By default, the audio file loads only when the user clicks play:

0:00 / 0:00
<Audio src="/assets/audio/example.mp3" />

Load the audio file when the component comes into view:

0:00 / 0:00
<Audio
src="/assets/audio/example.mp3"
preload="auto"
preloadDistance={200}
/>

Load the audio file immediately when the component mounts:

0:00 / 0:00
<Audio src="/assets/audio/example.mp3" preload="eager" />

The audio component emits several custom events that you can listen to:

0:00 / 0:00
Waiting for events… (Interact with the audio player to see events)
Audio Component Events
Event Details Description
play void Started audio playback
pause void Paused audio playback
timeupdate { currentTime: number; duration: number; } Time updated
volumechange { volume: number; muted: boolean; } Volume changed
Required Props
Audio Component Extends <div> HTMLElement
Prop Type Default Description
src string - URL of the audio file to play
audioProps HTMLAttributesWithData<'audio'> HTMLAudioElement attributes
preload 'none' | 'auto' | 'eager' none Loading strategy
preloadDistance number 100 Distance in pixels for auto preload
variant 'default' | 'underline' | 'minimal' | 'pill' default Visual style variant

The audio component exposes several methods for programmatic control:

Audio Component Functions
Function Type Description
play () => Promise<void> Start audio playback
pause () => void Pause audio playback
seek (time: number) => void Jump to specific time in seconds
volume set: (value: number) => void Set volume (0-1)
muted set: (value: boolean) => void Set muted state
Key Action
Space Play/Pause toggle
Left Arrow Seek backward 5 seconds
Right Arrow Seek forward 5 seconds
Up/Down Arrow Adjust volume
M Toggle mute

Limited Time Launch Sale

Enhance your web applications with our Audio component. Get the AstroJS starter template now and access a complete UI toolkit!

GET 60% OFF!
North Star Themes Logo

Subscribe to our newsletter

Get the latest AstroJS tips and tricks right to your inbox

Email: [email protected]

© 2025 North Star Themes

Web Kit Provided By North Star Themes