From 0d8034e23d6570c053433459d8a5174dc663b5d8 Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Mon, 15 Dec 2025 23:30:29 -0500 Subject: Add AudioPlayingHandle --- crates/teleia/src/audio.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/teleia/src/audio.rs b/crates/teleia/src/audio.rs index 087b63e..cac010b 100644 --- a/crates/teleia/src/audio.rs +++ b/crates/teleia/src/audio.rs @@ -19,6 +19,18 @@ impl Context { } } +#[cfg(target_arch = "wasm32")] +pub struct AudioPlayingHandle { + node: web_sys::AudioBufferSourceNode +} + +#[cfg(target_arch = "wasm32")] +impl AudioPlayingHandle { + pub fn stop(&self) { + self.node.stop().expect("failed to stop audio"); + } +} + #[cfg(target_arch = "wasm32")] pub struct Audio { pub buffer: Arc>>, @@ -52,7 +64,7 @@ impl Audio { } } - pub fn play(&self, ctx: &Context, looping: Option<(Option, Option)>) -> Option { + pub fn play(&self, ctx: &Context, looping: Option<(Option, Option)>) -> Option { let source = ctx.audio.create_buffer_source().ok()?; if let Some(ab) = &*self.buffer.lock().unwrap() { source.set_buffer(Some(&ab)); @@ -64,7 +76,7 @@ impl Audio { } source.connect_with_audio_node(&ctx.audio.destination()).ok()?; source.start().ok()?; - Some(source) + Some(AudioPlayingHandle { node: source }) } } -- cgit v1.2.3