Skip to content

Commit

Permalink
Fix: allow "playing" WebAudioPlayer w/o a URL (#3585)
Browse files Browse the repository at this point in the history
* Fix: allow "playing" WebAudioPlayer w/o a URL

* Reset _duration when src is set
  • Loading branch information
katspaugh authored Mar 9, 2024
1 parent 5e8c07a commit e32e519
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class Player<T extends GeneralEventTypes> extends EventEmitter<T> {

/** Start playing the audio */
public async play(): Promise<void> {
if (!this.media.src) return
return this.media.play()
}

Expand Down
8 changes: 8 additions & 0 deletions src/wavesurfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,14 @@ class WaveSurfer extends Player<WaveSurferEvents> {
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)
Expand Down
15 changes: 11 additions & 4 deletions src/webaudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ class WebAudioPlayer extends EventEmitter<WebAudioPlayerEvents> {
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()
Expand All @@ -53,6 +54,7 @@ class WebAudioPlayer extends EventEmitter<WebAudioPlayerEvents> {

set src(value: string) {
this.currentSrc = value
this._duration = undefined

if (!value) {
this.buffer = null
Expand Down Expand Up @@ -89,7 +91,9 @@ class WebAudioPlayer extends EventEmitter<WebAudioPlayerEvents> {

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)

Expand Down Expand Up @@ -164,18 +168,21 @@ class WebAudioPlayer extends EventEmitter<WebAudioPlayerEvents> {
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() {
Expand Down

0 comments on commit e32e519

Please sign in to comment.