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
Section titled “Try it”Install
Section titled “Install”cargo add gpdoc-plugin-audioExample
Section titled “Example”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.
Compose an audio button
Section titled “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
Section titled “Anatomy”audio_controls(...) Supplies an AudioNode for each clip└── clip.play_button(cx) Opens the resolved audio URL └── child(...) Your button labelShow the filename on the play button
Section titled “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.
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
Section titled “Settings”Pass an AudioOptions value to audio(options) to configure the schema and playback defaults.
placement
Section titled “placement”Placement::Block by default. Choose Inline to place the node inside a paragraph.
audio(AudioOptions { placement: gpdoc_plugin::core::Placement::Inline, ..Default::default() })allow_base64
Section titled “allow_base64”Defaults to false. Enable this to accept base64 audio data URLs as sources.
audio(AudioOptions { allow_base64: true, ..Default::default() })controls
Section titled “controls”Defaults to true. Stores whether the player should display playback controls.
audio(AudioOptions { controls: false, ..Default::default() })autoplay
Section titled “autoplay”Defaults to false. Requests playback when the clip loads.
audio(AudioOptions { autoplay: true, ..Default::default() })repeat
Section titled “repeat”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() })preload
Section titled “preload”Defaults to the string "metadata". Set it to "none" to request that the player defer loading.
audio(AudioOptions { preload: "none".into(), ..Default::default() })disable_remote_playback
Section titled “disable_remote_playback”Defaults to false. Sets the disableremoteplayback flag for players that support remote playback.
audio(AudioOptions { disable_remote_playback: true, ..Default::default() })Commands
Section titled “Commands”setAudio
Section titled “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.
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.