diff options
| author | LLLL Colonq <llll@colonq> | 2025-12-15 23:30:29 -0500 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2025-12-15 23:30:29 -0500 |
| commit | 0d8034e23d6570c053433459d8a5174dc663b5d8 (patch) | |
| tree | 410e49ba44051db1e7fa7d9cbb08fe4d48693538 | |
| parent | cd07af430b55c93e7f788dc0592a8fac04dc1b21 (diff) | |
Add AudioPlayingHandle
| -rw-r--r-- | crates/teleia/src/audio.rs | 16 |
1 files changed, 14 insertions, 2 deletions
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 @@ -20,6 +20,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<Mutex<Option<web_sys::AudioBuffer>>>, } @@ -52,7 +64,7 @@ impl Audio { } } - pub fn play(&self, ctx: &Context, looping: Option<(Option<f64>, Option<f64>)>) -> Option<web_sys::AudioBufferSourceNode> { + pub fn play(&self, ctx: &Context, looping: Option<(Option<f64>, Option<f64>)>) -> Option<AudioPlayingHandle> { 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 }) } } |
