From b608ec67991c7b3472a33d8b669528adb7d336bc Mon Sep 17 00:00:00 2001 From: Araozu Date: Thu, 10 Oct 2024 07:20:19 -0500 Subject: [PATCH] feat: preload next song --- src/utils/utils.templ | 69 ++++++++++++++++++++++++++++++++++++++-- src/utils/utils_templ.go | 20 ++++++------ 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/utils/utils.templ b/src/utils/utils.templ index 8c5f0a5..c383154 100644 --- a/src/utils/utils.templ +++ b/src/utils/utils.templ @@ -34,7 +34,7 @@ templ MusicPlayer() { x-data="player" >
- +

{ + const length = sound.duration(); + const targetLength = length - 5; + preloadInterval = setInterval(() => { + const pos = sound.seek(); + if (pos > targetLength) { + this.preload(); + clearInterval(preloadInterval); + } + }, 1000); this.loading = false; this.playing = true; }); + sound.once("end", () => { + this.playing = false; + this.next(); + }); this.currentSound = sound; }, + async playNext() { + this.currentSound?.unload(); + this.nextSound.play(); + this.playing = true; + this.currentSound = this.nextSound; + this.nextSound = null; + }, togglePlayPause() { if (this.playing === true) { this.playing = false; @@ -120,14 +143,54 @@ templ MusicPlayer() { next() { if (this.idx + 1 < this.queue.length) { this.idx += 1; - this.play(); + this.playNext(); } + }, + preload() { + console.log("preloading"); + if (!(this.idx + 1 < this.queue.length)) { + return + } + + const nextSongId = this.queue[this.idx + 1].songId; + const nextSound = new Howl({ + src: `https://navidrome.araozu.dev/rest/stream.view?id=${nextSongId}&v=1.13.0&c=music-to-go&u=fernando&s=49805d&t=4148cd1c83ae1bd01334facf4e70a947`, + html5: true, + volume: 0, + preload: true, + }); + // Attempt to play immediately the song, immediately pause it, rewind it and set volume back up + nextSound.play(); + + let preloadInterval; + nextSound.once("load", () => { + nextSound.pause(); + nextSound.seek(0); + nextSound.volume(this.volume); + + const length = nextSound.duration(); + const targetLength = length - 5; + preloadInterval = setInterval(() => { + const pos = nextSound.seek(); + if (pos > targetLength) { + this.preload(); + clearInterval(preloadInterval); + } + }, 1000); + this.loading = false; + this.playing = true; + }); + nextSound.once("end", () => { + this.playing = false; + this.next(); + }); + this.nextSound = nextSound; } })); }) function wait(ms) { - return new Promise(r => setTimeout(r, m)); + return new Promise(r => setTimeout(r, ms)); }

diff --git a/src/utils/utils_templ.go b/src/utils/utils_templ.go index 3ad2130..63d51ec 100644 --- a/src/utils/utils_templ.go +++ b/src/utils/utils_templ.go @@ -68,7 +68,7 @@ func MusicPlayer() templ.Component { templ_7745c5c3_Var2 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

-

-

") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

-

-

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -92,7 +92,7 @@ func MusicPlayer() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -128,7 +128,7 @@ func playIcon(size int) templ.Component { var templ_7745c5c3_Var4 string templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(size)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 140, Col: 28} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 203, Col: 28} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { @@ -141,7 +141,7 @@ func playIcon(size int) templ.Component { var templ_7745c5c3_Var5 string templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(size)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 141, Col: 29} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 204, Col: 29} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) if templ_7745c5c3_Err != nil { @@ -183,7 +183,7 @@ func pauseIcon(size int) templ.Component { var templ_7745c5c3_Var7 string templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(size)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 161, Col: 28} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 224, Col: 28} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) if templ_7745c5c3_Err != nil { @@ -196,7 +196,7 @@ func pauseIcon(size int) templ.Component { var templ_7745c5c3_Var8 string templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(size)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 162, Col: 29} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 225, Col: 29} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) if templ_7745c5c3_Err != nil { @@ -238,7 +238,7 @@ func skipForwardIcon(size int) templ.Component { var templ_7745c5c3_Var10 string templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(size)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 170, Col: 67} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 233, Col: 67} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10)) if templ_7745c5c3_Err != nil { @@ -251,7 +251,7 @@ func skipForwardIcon(size int) templ.Component { var templ_7745c5c3_Var11 string templ_7745c5c3_Var11, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(size)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 170, Col: 97} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 233, Col: 97} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var11)) if templ_7745c5c3_Err != nil { @@ -293,7 +293,7 @@ func circleNotchIcon(size int) templ.Component { var templ_7745c5c3_Var13 string templ_7745c5c3_Var13, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(size)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 184, Col: 28} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 247, Col: 28} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var13)) if templ_7745c5c3_Err != nil { @@ -306,7 +306,7 @@ func circleNotchIcon(size int) templ.Component { var templ_7745c5c3_Var14 string templ_7745c5c3_Var14, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(size)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 185, Col: 29} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/utils/utils.templ`, Line: 248, Col: 29} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var14)) if templ_7745c5c3_Err != nil {