From 09f774e739628c529086fd66a88d676371979732 Mon Sep 17 00:00:00 2001 From: xinyuaixiaoyu <38575991+xinyuaixiaoyu@users.noreply.github.com> Date: Tue, 14 Nov 2023 22:55:27 +0800 Subject: [PATCH] Feat: add stopAT in WebAudioPlayer (#3325) * Update webaudio.ts stopAT Add function for precise playback * Update webaudio.ts add timeSeconds type --- src/webaudio.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/webaudio.ts b/src/webaudio.ts index 0aa7ff0a1..921fac35f 100644 --- a/src/webaudio.ts +++ b/src/webaudio.ts @@ -100,6 +100,16 @@ class WebAudioPlayer extends EventEmitter { this.emit('pause') } + stopAt(timeSeconds: number) { + const delay = timeSeconds - this.currentTime + this.bufferNode?.stop(this.audioContext.currentTime + delay) + + this.bufferNode?.addEventListener('ended', () => { + this.bufferNode = null + this.pause() + }, { once: true }) + } + async setSinkId(deviceId: string) { const ac = this.audioContext as AudioContext & { setSinkId: (id: string) => Promise } return ac.setSinkId(deviceId)