Mosmox UI
Current version
GitHubnpmjs.com
Content & Media

Audio Player

Accessible audio playback with custom controls and metadata.

AudioPlayer is a responsive audio component with a custom control surface for playback, seeking, volume, mute, speed and downloading.

See the live Audio Player showcase.

Basic usage

<AudioPlayer
  src="/media/episode.mp3"
  title="Episode one"
  artist="Mosmox audio"
  artwork="/media/episode-cover.jpg"
/>

The player fills its parent width by default. Use width for a fixed or maximum layout and size for control density. artwork, title and artist are optional metadata shown above the controls.

Sources and fallback

Use sources to provide format alternatives. The browser selects the first supported source.

<AudioPlayer sources={[
  { src: '/media/episode.ogg', type: 'audio/ogg' },
  { src: '/media/episode.mp3', type: 'audio/mpeg' },
]} />

When the source is missing or cannot be loaded, fallback is displayed and onError is called.

Use either src for one file or sources for ordered format alternatives. When sources is provided, each source may include a MIME type so the browser can select a supported format. If no source is available, the player renders fallback instead of the controls.

Playback and controls

The default control surface includes progress, play/pause, rewind and fast-forward buttons, volume, mute, elapsed/duration text and playback speed. skipSeconds controls both seek buttons and the Left/Right keyboard shortcuts.

<AudioPlayer
  src="/media/episode.mp3"
  skipSeconds={15}
  rates={[0.75, 1, 1.25, 1.5, 2]}
  showVolumeControl
  showPlaybackRateControl
  download
/>

Keyboard shortcuts while the player is focused:

  • Space or K — play/pause;
  • ArrowLeft and ArrowRight — seek by skipSeconds;
  • M — mute/unmute.

Autoplay, loop and preload

autoPlay, loop, muted and preload map to native audio behavior. Browsers commonly block autoplay with sound, so autoplay should normally also use muted.

<AudioPlayer src="/media/ambient.mp3" autoPlay muted loop preload="auto" />

defaultVolume is a number from 0 to 1. defaultPlaybackRate selects the initial speed from the available rates list.

The component keeps playback state internally and exposes lifecycle callbacks for integration with external analytics or application state. onTimeUpdate receives the current position and duration in seconds; seeking through the progress bar and skip buttons also updates this callback.

Compact and large layouts

Use compact when metadata and artwork are unnecessary. The compact mode displays only the controls. size accepts sm, default and lg.

<AudioPlayer compact src="/media/short.mp3" />
<AudioPlayer size="lg" src="/media/album.mp3" artwork="/media/album.jpg" />

API Reference

PropValuesDefaultDescription
srcstring—Single audio source.
sourcesAudioSource[]—Ordered sources with optional MIME types.
titleReactNodeAudio trackTrack title.
artistReactNode—Artist or secondary metadata.
artworkstring—Artwork URL.
aria-labelstringAudio playerAccessible label for the player region.
widthnumber | string100%Player width.
sizesm | default | lgdefaultControl and metadata density.
compactbooleanfalseHides artwork and metadata.
autoPlaybooleanfalseStarts playback when allowed.
loopbooleanfalseRestarts after ending.
mutedbooleanfalseStarts muted.
defaultVolumenumber1Initial volume from 0 to 1.
defaultPlaybackRatenumber1Initial playback speed.
ratesnumber[][0.75, 1, 1.25, 1.5, 2]Playback speed options.
skipSecondsnumber10Seconds used by seek buttons and arrow keys.
preloadnone | metadata | autometadataNative preload hint.
showVolumeControlbooleantrueShows volume and mute controls.
showPlaybackRateControlbooleantrueShows playback speed menu.
downloadbooleanfalseShows a download link when src is available.
onPlay() => void—Called when playback begins.
onPause() => void—Called when playback pauses.
onEnded() => void—Called when playback ends.
onTimeUpdate(currentTime, duration) => void—Called during playback and seeking.
onErroraudio error handler—Called when loading fails.
fallbackReactNodeAudio is unavailableError or empty-state content.
classNamestring—Additional wrapper class.
styleCSSProperties—Inline wrapper styles.

AudioSource

FieldTypeDescription
srcstringAudio URL.
typestringOptional MIME type such as audio/mpeg.

Accessibility

The player is a focusable labelled region. All controls have accessible names, sliders have labels, and keyboard shortcuts provide playback and seeking without a pointer. Provide meaningful titles and artwork descriptions in surrounding content when the audio is part of a larger media collection.