Media Overlay: add stop() method

And don't remove highlight when pausing
This commit is contained in:
John Factotum
2024-09-30 19:31:14 +08:00
parent bf9adef636
commit e384aaf6dd
+12 -7
View File
@@ -443,11 +443,8 @@ class MediaOverlay extends EventTarget {
this.dispatchEvent(new CustomEvent('unhighlight', { detail: this.#activeItem })) this.dispatchEvent(new CustomEvent('unhighlight', { detail: this.#activeItem }))
} }
async #play(audioIndex, itemIndex) { async #play(audioIndex, itemIndex) {
if (this.#audio) { const paused = this.#audio?.paused
this.#audio.pause() if (this.#audio) this.stop()
URL.revokeObjectURL(this.#audio.src)
this.#audio = null
}
this.#audioIndex = audioIndex this.#audioIndex = audioIndex
this.#itemIndex = itemIndex this.#itemIndex = itemIndex
const src = this.#activeAudio?.src const src = this.#activeAudio?.src
@@ -477,14 +474,14 @@ class MediaOverlay extends EventTarget {
audio.addEventListener('error', () => audio.addEventListener('error', () =>
this.#error(new Error(`Failed to load ${src}`))) this.#error(new Error(`Failed to load ${src}`)))
audio.addEventListener('playing', () => this.#highlight()) audio.addEventListener('playing', () => this.#highlight())
audio.addEventListener('pause', () => this.#unhighlight())
audio.addEventListener('ended', () => { audio.addEventListener('ended', () => {
this.#unhighlight() this.#unhighlight()
URL.revokeObjectURL(url) URL.revokeObjectURL(url)
this.#audio = null this.#audio = null
this.#play(audioIndex + 1, 0).catch(e => this.#error(e)) this.#play(audioIndex + 1, 0).catch(e => this.#error(e))
}) })
audio.addEventListener('canplaythrough', () => if (paused) this.#highlight()
else audio.addEventListener('canplaythrough', () =>
audio.play().catch(e => this.#error(e)), { once: true }) audio.play().catch(e => this.#error(e)), { once: true })
} }
async start(sectionIndex, filter = () => true) { async start(sectionIndex, filter = () => true) {
@@ -512,6 +509,14 @@ class MediaOverlay extends EventTarget {
resume() { resume() {
this.#audio?.play().catch(e => this.#error(e)) this.#audio?.play().catch(e => this.#error(e))
} }
stop() {
if (this.#audio) {
this.#audio.pause()
URL.revokeObjectURL(this.#audio.src)
this.#audio = null
this.#unhighlight()
}
}
prev() { prev() {
if (this.#itemIndex > 0) this.#play(this.#audioIndex, this.#itemIndex - 1) if (this.#itemIndex > 0) this.#play(this.#audioIndex, this.#itemIndex - 1)
else if (this.#audioIndex > 0) this.#play(this.#audioIndex - 1, else if (this.#audioIndex > 0) this.#play(this.#audioIndex - 1,