-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from akashic-games/refactor-html-audio-player
refactor: refactor HTMLAudioPlayer
- Loading branch information
Showing
2 changed files
with
173 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* eslint-disable @typescript-eslint/typedef */ | ||
/* eslint-disable @typescript-eslint/explicit-function-return-type */ | ||
|
||
import { Trigger } from "@akashic/trigger"; | ||
import { setupChromeMEIWorkaround } from "./HTMLAudioAutoplayHelper"; | ||
|
||
export interface AudioElementPlayerParameterObject { | ||
id: string; | ||
element: HTMLAudioElement | null; | ||
duration: number; | ||
offset: number; | ||
loop: boolean; | ||
loopOffset: number; | ||
} | ||
|
||
export class AudioElementPlayer { | ||
onStop: Trigger<void>; | ||
|
||
id: string; | ||
element: HTMLAudioElement | null; | ||
offsetStart: number; | ||
offsetEnd: number; | ||
loopOffset: number; | ||
duration: number; | ||
loop: boolean; | ||
|
||
_dummyTimerId: number | null; | ||
_reachEndTimerId: number | null; | ||
_previousCurrentTime: number; | ||
|
||
constructor({ id, element, duration, offset, loopOffset, loop }: AudioElementPlayerParameterObject) { | ||
this.id = id; | ||
this.duration = duration; | ||
this.offsetStart = offset; | ||
this.offsetEnd = offset + duration; | ||
this.loop = loop; | ||
this.loopOffset = loopOffset; | ||
this.onStop = new Trigger(); | ||
this._dummyTimerId = null; | ||
this._reachEndTimerId = null; | ||
|
||
if (element) { | ||
setupChromeMEIWorkaround(element); | ||
element.addEventListener("timeupdate", this._onTimeupdate, false); | ||
element.addEventListener("ended", this._onEnded, false); | ||
element.currentTime = this.offsetStart / 1000; | ||
} else { | ||
if (!loop && duration != null) { | ||
this._setDummyTimer(this.duration); | ||
} | ||
} | ||
this.element = element; | ||
this._previousCurrentTime = element?.currentTime ?? 0; | ||
} | ||
|
||
rewind() { | ||
this.pause(); | ||
this.setCurrentTime(this.loopOffset || this.offsetStart); | ||
this.play(); | ||
} | ||
|
||
play() { | ||
this.element?.play().catch((_err) => { | ||
// user interact の前に play() を呼ぶとエラーになる。これは HTMLAudioAutoplayHelper で吸収する | ||
}); | ||
} | ||
|
||
pause() { | ||
this.element?.pause(); | ||
this._clearRewindTimer(); | ||
} | ||
|
||
setCurrentTime(offset: number) { | ||
if (!this.element) return; | ||
this.element.currentTime = offset / 1000; | ||
this._previousCurrentTime = this.element.currentTime; | ||
} | ||
|
||
setVolume(volume: number) { | ||
if (!this.element) return; | ||
this.element.volume = volume; | ||
} | ||
|
||
destroy() { | ||
this.onStop.destroy(); | ||
const element = this.element; | ||
if (element) { | ||
element.removeEventListener("timeupdate", this._onTimeupdate, false); | ||
element.removeEventListener("ended", this._onEnded, false); | ||
} | ||
this._clearDummyTimer(); | ||
this._clearRewindTimer(); | ||
} | ||
|
||
_setDummyTimer(duration: number) { | ||
this._clearDummyTimer(); | ||
this._dummyTimerId = window.setTimeout(() => this.pause(), duration); | ||
} | ||
|
||
_clearDummyTimer() { | ||
if (this._dummyTimerId == null) return; | ||
window.clearTimeout(this._dummyTimerId); | ||
this._dummyTimerId = null; | ||
} | ||
|
||
_setRewindTimer(duration: number) { | ||
this._clearRewindTimer(); | ||
this._reachEndTimerId = window.setTimeout(() => this._onReachEnd(), duration); | ||
} | ||
|
||
_clearRewindTimer() { | ||
if (this._reachEndTimerId == null) return; | ||
window.clearTimeout(this._reachEndTimerId); | ||
this._reachEndTimerId = null; | ||
} | ||
|
||
_onReachEnd() { | ||
if (this.loop) { | ||
this.rewind(); | ||
} else { | ||
this.pause(); | ||
this.onStop.fire(); | ||
} | ||
|
||
this._clearRewindTimer(); | ||
} | ||
|
||
_onTimeupdate = () => { | ||
const element = this.element!; // this.element が存在する場合にのみ呼び出される | ||
if (element.paused) return; | ||
|
||
const currentOffset = element.currentTime * 1000; | ||
const deltaSinceLastCall = Math.max(element.currentTime * 1000 - this._previousCurrentTime, 0); | ||
const deltaUntilEnd = Math.max(this.offsetEnd - element.currentTime * 1000, 0); | ||
this._previousCurrentTime = element.currentTime * 1000; | ||
|
||
if (this.offsetEnd < currentOffset + deltaSinceLastCall || this.offsetEnd <= currentOffset) { | ||
this._setRewindTimer(deltaUntilEnd); | ||
} | ||
}; | ||
|
||
_onEnded = () => { | ||
this._onReachEnd(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters