diff --git a/src/player.ts b/src/player.ts index 7d82c20f8..82dee7f65 100644 --- a/src/player.ts +++ b/src/player.ts @@ -92,7 +92,6 @@ class Player extends EventEmitter { /** Start playing the audio */ public async play(): Promise { - if (!this.media.src) return return this.media.play() } diff --git a/src/wavesurfer.ts b/src/wavesurfer.ts index fec658eaf..153134aa5 100644 --- a/src/wavesurfer.ts +++ b/src/wavesurfer.ts @@ -399,6 +399,14 @@ class WaveSurfer extends Player { this.onceMediaEvent('loadedmetadata', () => resolve(this.getDuration())) })) + // Set the duration if the player is a WebAudioPlayer without a URL + if (!url && !blob) { + const media = this.getMediaElement() + if (media instanceof WebAudioPlayer) { + media.duration = audioDuration + } + } + // Decode the audio data or use user-provided peaks if (channelData) { this.decodedData = Decoder.createBuffer(channelData, audioDuration || 0) diff --git a/src/webaudio.ts b/src/webaudio.ts index ed4aa16d0..330f5cf90 100644 --- a/src/webaudio.ts +++ b/src/webaudio.ts @@ -19,16 +19,17 @@ class WebAudioPlayer extends EventEmitter { private audioContext: AudioContext private gainNode: GainNode private bufferNode: AudioBufferSourceNode | null = null - private autoplay = false private playStartTime = 0 private playedDuration = 0 private _muted = false private _playbackRate = 1 + private _duration: number | undefined = undefined private buffer: AudioBuffer | null = null public currentSrc = '' public paused = true public crossOrigin: string | null = null public seeking = false + public autoplay = false constructor(audioContext = new AudioContext()) { super() @@ -53,6 +54,7 @@ class WebAudioPlayer extends EventEmitter { set src(value: string) { this.currentSrc = value + this._duration = undefined if (!value) { this.buffer = null @@ -89,7 +91,9 @@ class WebAudioPlayer extends EventEmitter { this.bufferNode?.disconnect() this.bufferNode = this.audioContext.createBufferSource() - this.bufferNode.buffer = this.buffer + if (this.buffer) { + this.bufferNode.buffer = this.buffer + } this.bufferNode.playbackRate.value = this._playbackRate this.bufferNode.connect(this.gainNode) @@ -164,18 +168,21 @@ class WebAudioPlayer extends EventEmitter { return time * this._playbackRate } set currentTime(value) { - this.emit('seeking') const wasPlaying = !this.paused wasPlaying && this._pause() this.playedDuration = value / this._playbackRate wasPlaying && this._play() + this.emit('seeking') this.emit('timeupdate') } get duration() { - return this.buffer?.duration || 0 + return this._duration ?? (this.buffer?.duration || 0) + } + set duration(value: number) { + this._duration = value } get volume() {