Fix: Make stream folder recursive so it never stops playing
This commit is contained in:
parent
8c836ef181
commit
a8ab7694a9
1 changed files with 25 additions and 22 deletions
47
server.go
47
server.go
|
@ -163,13 +163,31 @@ func getFileDelay(mp3FilePath string) int64 {
|
|||
return delayVal
|
||||
}
|
||||
|
||||
func streamFolder(connectionPool *ConnectionPool, metadataConnectionPool *MetadataConnectionPool, list []string) {
|
||||
func streamFolder(connectionPool *ConnectionPool, metadataConnectionPool *MetadataConnectionPool, folder string) {
|
||||
|
||||
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 {
|
||||
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(buffer)
|
||||
tempfile := bytes.NewReader(ctn)
|
||||
delay := getFileDelay(filepath.Join("./music/", music))
|
||||
delay := getFileDelay(filepath.Join(folder, music))
|
||||
ticker := time.NewTicker(time.Millisecond * time.Duration(delay))
|
||||
|
||||
// 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() {
|
||||
|
@ -238,25 +258,8 @@ func main() {
|
|||
metadataConnPool := NewMetadataConnectionPool()
|
||||
peopleConnPool := NewPeopleConnectionPool()
|
||||
|
||||
music_files := []string{}
|
||||
|
||||
entries, err := os.ReadDir("./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)
|
||||
go streamFolder(connPool, metadataConnPool, "./music")
|
||||
|
||||
http.Handle("/", http.FileServer(http.Dir("./dist")))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue