# LazyAudio

Audio media that picks the media component for its source and loads only that component's code

> **Caution: Unstable API**
>
> This API works today, but its shape is still settling and may change in a minor release. The implementation underneath is production quality; what is not final is how you configure it from Video.js. Pin your version and check the changelog when you upgrade.

The audio counterpart of [LazyVideo](https://videojs.org/docs/framework/react/reference/components/lazy-video): reads `src`, renders the media component that plays it, and loads that component’s code only once the source is known.

## Import

```bash
npm install @videojs/react @videojs/mux-audio @videojs/spotify-audio @videojs/soundcloud-audio @videojs/mixcloud-audio
```

```tsx
import { LazyAudio } from '@videojs/react/media/lazy-audio';
```

## Sources

| Source | Resolves to | Loads |
| --- | --- | --- |
| Audio and video files | Native `<audio>` | Nothing |
| HLS | [HlsAudio](https://videojs.org/docs/framework/react/reference/components/hls-audio) | The SPF HLS engine |
| HLS where there’s no MSE | Native `<audio>` | Nothing |
| `stream.mux.com` | [MuxAudio](https://videojs.org/docs/framework/react/reference/components/mux-audio) | Mux Audio |
| Spotify | [SpotifyAudio](https://videojs.org/docs/framework/react/reference/components/spotify-audio) | The Spotify embed |
| SoundCloud | [SoundCloudAudio](https://videojs.org/docs/framework/react/reference/components/soundcloud-audio) | The SoundCloud widget |
| Mixcloud | [MixcloudAudio](https://videojs.org/docs/framework/react/reference/components/mixcloud-audio) | The Mixcloud widget |

A source that matches none of these, including YouTube, Vimeo, and DASH, renders nothing and logs an error in development.

## Custom media

`createLazyAudio` builds a `LazyAudio` from a list of media, tried in order, the same way [`createLazyVideo`](https://videojs.org/docs/framework/react/reference/components/lazy-video) does. The built-in media are `audio`, `hlsAudio`, `muxAudio`, `spotifyAudio`, `soundcloudAudio`, and `mixcloudAudio`, each imported from `@videojs/react/media/lazy/` plus its kebab-case name.

```tsx
import { audio } from '@videojs/react/media/lazy/audio';
import { createLazyAudio } from '@videojs/react/media/lazy/create';
import { muxAudio } from '@videojs/react/media/lazy/mux-audio';

const MuxLazyAudio = createLazyAudio([audio, muxAudio]);
```

`LazyAudio` takes the same `src`, `type`, `preferPlayback`, and `preload` options as `LazyVideo`, and loads and passes props on the same way. See [LazyVideo](https://videojs.org/docs/framework/react/reference/components/lazy-video) for its behavior and API reference.

## Examples

### Basic Usage

**App.tsx**

```tsx
import { LazyAudio } from '@videojs/react/media/lazy-audio';

import './BasicUsage.css';

export default function BasicUsage() {
  return <LazyAudio className="react-lazy-audio-basic" src="https://stream.mux.com/BV3YZtogl89mg9VcNBhhnHm02Y34zI1nlMuMQfAbl3dM/audio.m4a" controls preload="metadata" />;
}
```

**App.css**

```css
.react-lazy-audio-basic {
  width: 100%;
}
```

---

React documentation: https://videojs.org/docs/framework/react/llms.txt
All documentation: https://videojs.org/llms.txt
