Skip to content

Commit 9fcd847

Browse files
committed
Fix: rm canplay event
1 parent 9931a67 commit 9fcd847

File tree

4 files changed

+15
-35
lines changed

4 files changed

+15
-35
lines changed

examples/envelope.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ wavesurfer.on('timeupdate', () => {
4747

4848
// Play/pause button
4949
const button = document.querySelector('button')
50-
wavesurfer.once('canplay', () => {
50+
wavesurfer.once('ready', () => {
5151
button.onclick = () => {
5252
wavesurfer.isPlaying() ? wavesurfer.pause() : wavesurfer.play()
5353
}

examples/record.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ recButton.onclick = () => {
5858
}
5959

6060
// Play/pause
61-
wavesurfer.once('canplay', () => {
61+
wavesurfer.once('ready', () => {
6262
playButton.onclick = () => {
6363
wavesurfer.playPause()
6464
}

src/plugins/regions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ class RegionsPlugin extends BasePlugin<RegionsPluginEvents, RegionsPluginOptions
438438

439439
if (!duration) {
440440
this.subscriptions.push(
441-
this.wavesurfer.once('canplay', (duration) => {
441+
this.wavesurfer.once('ready', (duration) => {
442442
region._setTotalDuration(duration)
443443
this.saveRegion(region)
444444
}),

src/wavesurfer.ts

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ export type WaveSurferEvents = {
7474
load: [url: string]
7575
/** When the audio has been decoded */
7676
decode: [duration: number]
77-
/** When the media element has loaded enough to play */
78-
canplay: [duration: number]
7977
/** When the audio is both decoded and can play */
8078
ready: [duration: number]
8179
/** When a waveform is drawn */
@@ -112,7 +110,6 @@ class WaveSurfer extends Player<WaveSurferEvents> {
112110
private timer: Timer
113111
private plugins: GenericPlugin[] = []
114112
private decodedData: AudioBuffer | null = null
115-
private canPlay = false
116113
protected subscriptions: Array<() => void> = []
117114

118115
/** Create a new WaveSurfer instance */
@@ -137,7 +134,6 @@ class WaveSurfer extends Player<WaveSurferEvents> {
137134
this.initPlayerEvents()
138135
this.initRendererEvents()
139136
this.initTimerEvents()
140-
this.initReadyEvent()
141137
this.initPlugins()
142138

143139
const url = this.options.url || this.options.media?.currentSrc || this.options.media?.src
@@ -177,11 +173,6 @@ class WaveSurfer extends Player<WaveSurferEvents> {
177173
this.emit('finish')
178174
}),
179175

180-
this.onMediaEvent('canplay', () => {
181-
this.canPlay = true
182-
this.emit('canplay', this.getDuration())
183-
}),
184-
185176
this.onMediaEvent('seeking', () => {
186177
this.emit('seeking', this.getCurrentTime())
187178
}),
@@ -193,7 +184,7 @@ class WaveSurfer extends Player<WaveSurferEvents> {
193184
// Seek on click
194185
this.renderer.on('click', (relativeX) => {
195186
if (this.options.interact) {
196-
this.canPlay && this.seekTo(relativeX)
187+
this.seekTo(relativeX)
197188
this.emit('interaction')
198189
this.emit('click', relativeX)
199190
}
@@ -219,19 +210,17 @@ class WaveSurfer extends Player<WaveSurferEvents> {
219210
this.renderer.on('drag', (relativeX) => {
220211
if (!this.options.interact) return
221212

222-
if (this.canPlay) {
223-
// Update the visual position
224-
this.renderer.renderProgress(relativeX)
213+
// Update the visual position
214+
this.renderer.renderProgress(relativeX)
225215

226-
// Set the audio position with a debounce
227-
clearTimeout(debounce)
228-
debounce = setTimeout(
229-
() => {
230-
this.seekTo(relativeX)
231-
},
232-
this.isPlaying() ? 0 : 200,
233-
)
234-
}
216+
// Set the audio position with a debounce
217+
clearTimeout(debounce)
218+
debounce = setTimeout(
219+
() => {
220+
this.seekTo(relativeX)
221+
},
222+
this.isPlaying() ? 0 : 200,
223+
)
235224

236225
this.emit('interaction')
237226
this.emit('drag', relativeX)
@@ -252,15 +241,6 @@ class WaveSurfer extends Player<WaveSurferEvents> {
252241
)
253242
}
254243

255-
private initReadyEvent() {
256-
const emitReady = () => {
257-
if (this.decodedData && this.canPlay) {
258-
this.emit('ready', this.getDuration())
259-
}
260-
}
261-
this.subscriptions.push(this.on('decode', emitReady), this.on('canplay', emitReady))
262-
}
263-
264244
private initPlugins() {
265245
if (!this.options.plugins?.length) return
266246

@@ -294,7 +274,6 @@ class WaveSurfer extends Player<WaveSurferEvents> {
294274
/** Load an audio file by URL, with optional pre-decoded audio data */
295275
public async load(url: string, channelData?: WaveSurferOptions['peaks'], duration?: number) {
296276
this.decodedData = null
297-
this.canPlay = false
298277

299278
this.emit('load', url)
300279

@@ -319,6 +298,7 @@ class WaveSurfer extends Player<WaveSurferEvents> {
319298
}
320299

321300
this.emit('decode', this.getDuration())
301+
this.emit('ready', this.getDuration())
322302

323303
this.renderer.render(this.decodedData)
324304
}

0 commit comments

Comments
 (0)