# Audio — SvelteKit UI

Package: sveltekit-ui 1.1.94

Page: https://www.sveltekit-ui.com/components/audio

Category: Media

Manages playback of audio, including metadata, playback rate, volume, and skip time

Public component import: `Audio`

Public manager import: `create_audio_manager`

## Usage

```svelte
<script>
  // Import the shared stylesheet once in your application's root layout.
  import "sveltekit-ui/style.css"
  import { Audio, create_audio_manager } from "sveltekit-ui"

  const manager = create_audio_manager({
    src: "https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3",
    title: "T. rex roar",
    type: "audio/mpeg",
    ui_type: "standard",
  })
</script>

<Audio {manager} />
```

## Configuration

### `storage_path`

- type: string
- default: null
- description: template path for retrieving audio or image sources by id

### `src`

- type: string
- default: null
- description: direct audio source url, or retrieved via storage_id if null

### `storage_id`

- type: string
- default: null
- description: id used with storage_path if src is null

### `type`

- type: string
- default: audio/mpeg
- description: mime type for the audio source

### `title`

- type: string
- default: null
- description: title metadata for the audio

### `artist`

- type: string
- default: null
- description: artist metadata for the audio

### `album`

- type: string
- default: null
- description: album metadata for the audio

### `image_src`

- type: string
- default: null
- description: direct url for the audio artwork image

### `image_storage_id`

- type: string
- default: null
- description: id used with storage_path if image_src is null

### `image_type`

- type: string
- default: null
- description: mime type for the artwork image

### `default_skip_time`

- type: number
- default: null
- description: seconds to skip for nav_seek_backward / nav_seek_forward

### `ui_type`

- type: string
- default: standard
- description: layout style for the audio player (standard, short, or square)

### `ml`

- type: number
- default: 0
- description: margin-left in rem units

### `mr`

- type: number
- default: 0
- description: margin-right in rem units

### `mt`

- type: number
- default: 0
- description: margin-top in rem units

### `mb`

- type: number
- default: 0
- description: margin-bottom in rem units

### `is_hide_image`

- type: boolean
- default: false
- description: whether the audio image is hidden

## Methods

### `handle_mount()`

- description: executes on svelte onmount, checks audio readiness
- parameters:
- returns: void

### `handle_loaded_metadata()`

- description: sets the time slider max after metadata loads
- parameters:
- returns: void

### `handle_progress()`

- description: updates the buffered time for partial loading
- parameters:
- returns: void

### `handle_time_update()`

- description: syncs the current audio time with the slider manager
- parameters:
- returns: void

### `handle_rate_change()`

- description: updates the audio's playback rate
- parameters:
  - rate: number
- returns: void

### `handle_seeking()`

- description: indicates the audio is seeking; sets is_loading to true
- parameters:
- returns: void

### `handle_can_play()`

- description: indicates the audio can play; sets is_loading to false
- parameters:
- returns: void

### `seek_to_audio()`

- description: seeks the audio to a specified time
- parameters:
  - time: number
- returns: void

### `resize_image()`

- description: downscales an image to 128x128 if it exceeds that size
- parameters:
  - src: string
  - callback: function
- returns: void

### `setup_media_session()`

- description: configures media session metadata (title, album, artwork, etc.)
- parameters:
- returns: void

### `update_position_state()`

- description: updates position state within the media session
- parameters:
- returns: void

### `nav_seek_backward()`

- description: skips backward by a set or default number of seconds
- parameters:
  - details: object
- returns: void

### `nav_seek_forward()`

- description: skips forward by a set or default number of seconds
- parameters:
  - details: object
- returns: void

### `toggle_play_pause()`

- description: toggles audio state between play and pause
- parameters:
- returns: void

### `play_audio()`

- description: plays the audio if it is paused
- parameters:
- returns: void

### `pause_audio()`

- description: pauses the audio if it is playing
- parameters:
- returns: void

### `set_is_loading()`

- description: manually sets the is_loading state
- parameters:
  - input: boolean
- returns: void

## Returned state

### `id`

- type: string
- description: unique id generated for the audio element

### `src`

- type: string
- description: active audio source

### `type`

- type: string
- description: mime type for the audio source

### `title`

- type: string
- description: title metadata for the audio

### `artist`

- type: string
- description: artist metadata for the audio

### `album`

- type: string
- description: album metadata for the audio

### `image_src`

- type: string
- description: direct url for the audio artwork image

### `image_type`

- type: string
- description: mime type of the audio artwork image

### `default_skip_time`

- type: number
- description: default skip time in seconds for navigation

### `ui_type`

- type: string
- description: current layout style (standard, short, or square)

### `ml`

- type: number
- description: margin-left in rem units

### `mr`

- type: number
- description: margin-right in rem units

### `mt`

- type: number
- description: margin-top in rem units

### `mb`

- type: number
- description: margin-bottom in rem units

### `is_hide_image`

- type: boolean
- description: true if the audio image is hidden

### `play_pause_button_manager`

- type: object
- description: button manager for the play/pause control

### `time_slider_manager`

- type: object
- description: slider manager for the current time

### `settings_popover_manager`

- type: object
- description: popover manager for audio settings

### `settings_button_manager`

- type: object
- description: button manager for opening the settings popover

### `volume_slider_manager`

- type: object
- description: slider manager for volume adjustment

### `rate_slider_manager`

- type: object
- description: slider manager for playback rate adjustment

### `has_setup_media_session`

- type: boolean
- description: indicates whether media session metadata is set

### `formatted_duration`

- type: string
- description: formatted total duration of the audio

### `formatted_current_time`

- type: string
- description: formatted current time in playback

### `is_loading`

- type: boolean
- description: indicates if the audio is loading
