Skip to content

Commit

Permalink
Media Overlay: fix skipping
Browse files Browse the repository at this point in the history
- Fix not seeking when skipping to another section when paused
- Fix not playing when skipping when not paused in WebKit
  • Loading branch information
johnfactotum committed Oct 7, 2024
1 parent fa85f13 commit 7cbc36b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions epub.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ class MediaOverlay extends EventTarget {
#audio
#volume = 1
#rate = 1
#state
constructor(book, loadXML) {
super()
this.book = book
Expand Down Expand Up @@ -443,8 +444,7 @@ class MediaOverlay extends EventTarget {
this.dispatchEvent(new CustomEvent('unhighlight', { detail: this.#activeItem }))
}
async #play(audioIndex, itemIndex) {
const paused = this.#audio?.paused
if (this.#audio) this.stop()
this.#stop()
this.#audioIndex = audioIndex
this.#itemIndex = itemIndex
const src = this.#activeAudio?.src
Expand All @@ -453,7 +453,6 @@ class MediaOverlay extends EventTarget {
const url = URL.createObjectURL(await this.book.loadBlob(src))
const audio = new Audio(url)
this.#audio = audio
audio.currentTime = this.#activeItem.begin ?? 0
audio.volume = this.#volume
audio.playbackRate = this.#rate
audio.addEventListener('timeupdate', () => {
Expand All @@ -480,9 +479,17 @@ class MediaOverlay extends EventTarget {
this.#audio = null
this.#play(audioIndex + 1, 0).catch(e => this.#error(e))
})
if (paused) this.#highlight()
else audio.addEventListener('canplaythrough', () =>
audio.play().catch(e => this.#error(e)), { once: true })
if (this.#state === 'paused') {
this.#highlight()
audio.currentTime = this.#activeItem.begin ?? 0
}
else audio.addEventListener('canplaythrough', () => {
// for some reason need to seek in `canplaythrough`
// or it won't play when skipping in WebKit
audio.currentTime = this.#activeItem.begin ?? 0
this.#state = 'playing'
audio.play().catch(e => this.#error(e))
}, { once: true })
}
async start(sectionIndex, filter = () => true) {
this.#audio?.pause()
Expand All @@ -504,19 +511,25 @@ class MediaOverlay extends EventTarget {
}
}
pause() {
this.#state = 'paused'
this.#audio?.pause()
}
resume() {
this.#state = 'playing'
this.#audio?.play().catch(e => this.#error(e))
}
stop() {
#stop() {
if (this.#audio) {
this.#audio.pause()
URL.revokeObjectURL(this.#audio.src)
this.#audio = null
this.#unhighlight()
}
}
stop() {
this.#state = 'stopped'
this.#stop()
}
prev() {
if (this.#itemIndex > 0) this.#play(this.#audioIndex, this.#itemIndex - 1)
else if (this.#audioIndex > 0) this.#play(this.#audioIndex - 1,
Expand Down

0 comments on commit 7cbc36b

Please sign in to comment.