Skip to content

Commit

Permalink
loop check with setTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
kamakiri01 committed Dec 11, 2023
1 parent 2b4915a commit 9c18410
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/plugin/HTMLAudioPlugin/HTMLAudioPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,27 @@ export class HTMLAudioPlayer extends AudioPlayer {
});
}
if (end != null) {
const onEnded: () => void = () => {
if (asset.loop) {
audio.currentTime = loopStart;
} else {
audio.pause();
}
};
const setTimer = function(): void {
setTimeout(() => {
onEnded();
setTimer();
}, (end - loopStart) * 1000);
};
// timeupdate イベント通知の精度が低いため、 setTimeout と併用する
// addEventListener は裏タブ時にも動作させるため残す
audio.addEventListener("timeupdate", () => {
if (end <= audio.currentTime) {
if (asset.loop) {
audio.currentTime = loopStart;
} else {
audio.pause();
}
onEnded();
}
});
setTimer();
}
}

Expand Down

0 comments on commit 9c18410

Please sign in to comment.