Skip to content
gpdoc
Playground

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.

Terminal window
cargo add gpdoc-plugin-audio

Register an audio view with a play button in the Quickstart editor configuration.

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.

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

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

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

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.

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

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

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

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

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

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

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

Defaults to false. Requests playback when the clip loads.

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

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

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

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

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

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

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

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

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

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.

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.

audio/src/lib.rs