Skip to content

Commit

Permalink
Fix: handle empty src in the web audio player
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Dec 28, 2023
1 parent 62b26cb commit b22e8b5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/webaudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,21 @@ class WebAudioPlayer extends EventEmitter<WebAudioPlayerEvents> {
set src(value: string) {
this.currentSrc = value

if (!value) {
this.buffer = null
this.emit('emptied')
return
}

fetch(value)
.then((response) => response.arrayBuffer())
.then((arrayBuffer) => this.audioContext.decodeAudioData(arrayBuffer))
.then((arrayBuffer) => {
if (this.currentSrc !== value) return null
return this.audioContext.decodeAudioData(arrayBuffer)
})
.then((audioBuffer) => {
if (this.currentSrc !== value) return

this.buffer = audioBuffer

this.emit('loadedmetadata')
Expand Down

0 comments on commit b22e8b5

Please sign in to comment.