Fix: Make stream folder recursive so it never stops playing

This commit is contained in:
zuma 2025-03-18 18:38:18 +01:00
parent 8c836ef181
commit a8ab7694a9

View file

@ -163,13 +163,31 @@ func getFileDelay(mp3FilePath string) int64 {
return delayVal return delayVal
} }
func streamFolder(connectionPool *ConnectionPool, metadataConnectionPool *MetadataConnectionPool, list []string) { func streamFolder(connectionPool *ConnectionPool, metadataConnectionPool *MetadataConnectionPool, folder string) {
buffer := make([]byte, BUFFERSIZE) buffer := make([]byte, BUFFERSIZE)
for _, music := range list { music_files := []string{}
file, err := os.Open(filepath.Join("./music/", music)) entries, err := os.ReadDir(folder)
if err != nil {
log.Fatal(err)
}
for _, e := range entries {
if filepath.Ext(e.Name()) == ".mp3" {
music_files = append(music_files, e.Name())
}
}
log.Printf("%d Music file found.", len(music_files))
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(music_files), func(i, j int) { music_files[i], music_files[j] = music_files[j], music_files[i] })
for _, music := range music_files {
file, err := os.Open(filepath.Join(folder, music))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -209,7 +227,7 @@ func streamFolder(connectionPool *ConnectionPool, metadataConnectionPool *Metada
// clear() is a new builtin function introduced in go 1.21. Just reinitialize the buffer if on a lower version. // clear() is a new builtin function introduced in go 1.21. Just reinitialize the buffer if on a lower version.
clear(buffer) clear(buffer)
tempfile := bytes.NewReader(ctn) tempfile := bytes.NewReader(ctn)
delay := getFileDelay(filepath.Join("./music/", music)) delay := getFileDelay(filepath.Join(folder, music))
ticker := time.NewTicker(time.Millisecond * time.Duration(delay)) ticker := time.NewTicker(time.Millisecond * time.Duration(delay))
// Send one buffer in advance to avoid client choking // Send one buffer in advance to avoid client choking
@ -231,6 +249,8 @@ func streamFolder(connectionPool *ConnectionPool, metadataConnectionPool *Metada
} }
streamFolder(connectionPool, metadataConnectionPool, folder)
} }
func main() { func main() {
@ -238,25 +258,8 @@ func main() {
metadataConnPool := NewMetadataConnectionPool() metadataConnPool := NewMetadataConnectionPool()
peopleConnPool := NewPeopleConnectionPool() peopleConnPool := NewPeopleConnectionPool()
music_files := []string{}
entries, err := os.ReadDir("./music") go streamFolder(connPool, metadataConnPool, "./music")
if err != nil {
log.Fatal(err)
}
for _, e := range entries {
if filepath.Ext(e.Name()) == ".mp3" {
music_files = append(music_files, e.Name())
}
}
log.Printf("%d Music file found.", len(music_files))
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(music_files), func(i, j int) { music_files[i], music_files[j] = music_files[j], music_files[i] })
go streamFolder(connPool, metadataConnPool, music_files)
http.Handle("/", http.FileServer(http.Dir("./dist"))) http.Handle("/", http.FileServer(http.Dir("./dist")))