# Audio

> Store an audio source and playback attributes in the document.

`audio(Default::default())` adds a block audio node. It stores the source and playback attributes, including whether to show controls, mute playback, or loop.

## Try it

[Try the audio editor example](https://gpdoc.dev/docs/plugins/media/audio/#audio-example)

## Install

```sh
cargo add gpdoc-plugin-audio
```

## Example

Register an audio view with a play button in the [Quickstart editor configuration](/docs/quickstart/#configure-the-editor).

```rust
use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};
use gpdoc_plugin_audio::{audio, audio_controls};
use gpdoc_plugin_paragraph::paragraph;
use gpui::prelude::*;


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(audio(Default::default()).with_view(player))
    .build()
    .expect("configure editor");


fn player() -> ViewPlugin {
    audio_controls(|clip, _, cx| clip.play_button(cx).child("Play"))
}
```

Paste an HTTP(S) URL ending in `.ogg` into an empty paragraph. Click **Play** to open the clip in the system handler.

## Compose an audio button

The default GPUI view displays the source in the document. Use `audio_controls` to place your own control inside the space that label occupies.

### Anatomy

```text
audio_controls(...)       Supplies an AudioNode for each clip
└── clip.play_button(cx)  Opens the resolved audio URL
    └── child(...)        Your button label
```

### Show the filename on the play button

Replace `player` in the example above. Read the filename from the stored source and put it inside the existing play button.

```rust
fn player() -> ViewPlugin {
    audio_controls(|clip, _, cx| {
        let name = clip.source()
            .filter(|src| !src.starts_with("data:"))
            .and_then(|src| src.rsplit('/').next())
            .filter(|name| !name.is_empty())
            .unwrap_or("Audio");
        clip.play_button(cx).child(format!("▶ {name}"))
    })
}
```

For `./media/interview.ogg`, the button reads **▶ interview\.ogg**. Set an HTTP(S) base with `EditorView::set_media_base_url` to resolve relative paths. Clicking the button opens the resolved HTTP(S) URL in the system handler. An in-app player can read the original source from `clip.source()` and the playback attributes from `clip.slot.node.attrs`.

## Settings

Pass an `AudioOptions` value to `audio(options)` to configure the schema and playback defaults.

### `placement`

`Placement::Block` by default. Choose `Inline` to place the node inside a paragraph.

```rust
audio(AudioOptions { placement: gpdoc_plugin::core::Placement::Inline, ..Default::default() })
```

### `allow_base64`

Defaults to `false`. Enable this to accept base64 audio data URLs as sources.

```rust
audio(AudioOptions { allow_base64: true, ..Default::default() })
```

### `controls`

Defaults to `true`. Stores whether the player should display playback controls.

```rust
audio(AudioOptions { controls: false, ..Default::default() })
```

### `autoplay`

Defaults to `false`. Requests playback when the clip loads.

```rust
audio(AudioOptions { autoplay: true, ..Default::default() })
```

### `repeat`

Defaults to `false`. Sets the saved `loop` flag on new audio nodes.

```rust
audio(AudioOptions { repeat: true, ..Default::default() })
```

### `muted`

Defaults to `false`. Sets the initial mute state for new clips.

```rust
audio(AudioOptions { muted: true, ..Default::default() })
```

### `preload`

Defaults to the string `"metadata"`. Set it to `"none"` to request that the player defer loading.

```rust
audio(AudioOptions { preload: "none".into(), ..Default::default() })
```

### `disable_remote_playback`

Defaults to `false`. Sets the `disableremoteplayback` flag for players that support remote playback.

```rust
audio(AudioOptions { disable_remote_playback: true, ..Default::default() })
```

## Commands

### `setAudio`

Inserts at the caret or replaces the current selection. Supply `src` as an HTTP(S) URL or a path starting with `/`, `./`, or `../`. With `allow_base64` enabled, the command also accepts audio data URLs. An unsupported source returns `CommandOutcome::NotApplicable`.

```rust
editor.run(gpdoc_plugin_audio::Set {
    src: "./media/interview.ogg".into(),
    muted: Some(true),
    ..Default::default()
})?;
```

Override config defaults for a single clip with the boolean attributes `controls`, `autoplay`, `loop`, `muted`, and `disableremoteplayback`, or with the string `preload`. The optional strings `controlslist` and `crossorigin` accept null and are omitted by default.

Pasting a web URL ending in a recognized suffix such as `.mp3` or `.ogg` also inserts an audio node. GPUI opens playback in the system handler. Use `EditorView::set_media_base_url` to resolve relative sources.

## Source code

[audio/src/lib.rs](/source/plugins/audio/src/lib.rs.txt)