Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: empty url with loadBlob #3719

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions cypress/e2e/options.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,4 +597,26 @@ describe('WaveSurfer options tests', () => {
})
})
})

it('should load a blob', () => {
cy.window().then((win) => {
const wavesurfer = win.WaveSurfer.create({
container: id,
height: 100,
})

const blob = Cypress.Blob.base64StringToBlob(
'UklGRuYAAABXQVZFZm10IBAAAAABAAEAgD4AAAB9AAACABAAZGF0YQAAAAA=',
'audio/wav',
)

wavesurfer.loadBlob(
blob,
Array.from({ length: 512 }).map((_, i) => Math.sin(i / 16)),
10,
)

cy.get(id).matchImageSnapshot('loadBlob')
})
})
})
Binary file added cypress/snapshots/loadBlob.snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class Player<T extends GeneralEventTypes> extends EventEmitter<T> {

protected setSrc(url: string, blob?: Blob) {
const src = this.getSrc()
if (src === url) return
if (url && src === url) return
this.revokeSrc()
const newSrc = blob instanceof Blob && this.canPlayType(blob.type) ? URL.createObjectURL(blob) : url
const newSrc = blob instanceof Blob && (this.canPlayType(blob.type) || !url) ? URL.createObjectURL(blob) : url
this.media.src = newSrc
}

Expand Down
2 changes: 1 addition & 1 deletion src/wavesurfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ class WaveSurfer extends Player<WaveSurferEvents> {
/** Load an audio blob */
public async loadBlob(blob: Blob, channelData?: WaveSurferOptions['peaks'], duration?: number) {
try {
return await this.loadAudio('blob', blob, channelData, duration)
return await this.loadAudio('', blob, channelData, duration)
} catch (err) {
this.emit('error', err as Error)
throw err
Expand Down
Loading