From 1f05c2bc4a9ae4791cd64662a5bc8fab98d778f1 Mon Sep 17 00:00:00 2001 From: xinyuaixiaoyu <38575991+xinyuaixiaoyu@users.noreply.github.com> Date: Wed, 8 Nov 2023 10:33:32 +0800 Subject: [PATCH] Update webaudio.ts stopAT Add function for precise playback --- src/webaudio.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/webaudio.ts b/src/webaudio.ts index 0aa7ff0a1..48af9091d 100644 --- a/src/webaudio.ts +++ b/src/webaudio.ts @@ -100,6 +100,16 @@ class WebAudioPlayer extends EventEmitter { this.emit('pause') } + stopAt(timeSeconds) { + 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)