aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-05-21 21:24:27 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2023-05-21 21:24:27 +0200
commit70279c4c62ae4180706c0d27c5bbcf275c903100 (patch)
tree486c471b61618a96d98d8603db188c2101dbb7bd
parent95a65d718c3bc50324a0f4a75aa5a586eeadc4ef (diff)
downloadWRMS-70279c4c62ae4180706c0d27c5bbcf275c903100.tar.gz
WRMS-70279c4c62ae4180706c0d27c5bbcf275c903100.zip
continue playback when adding a song to an empty queue
Now WRMS only switches the playback state when an admin calls the /playpause endpoint. When the queue runs out of songs the state is kept and continues when a new song is added.
-rw-r--r--TODO.md2
-rw-r--r--main.go1
-rw-r--r--player.go6
-rw-r--r--upload.go1
-rw-r--r--web/client.html1
-rw-r--r--wrms.go35
6 files changed, 24 insertions, 22 deletions
diff --git a/TODO.md b/TODO.md
index d9c27b9..ccbf499 100644
--- a/TODO.md
+++ b/TODO.md
@@ -2,7 +2,7 @@
- [X] add admin
- [ ] allow admin to delete songs
-- [ ] continue playback when song is added to empty playlist
+- [X] continue playback when song is added to empty playlist
## Fronted
diff --git a/main.go b/main.go
index 956f374..2b0a14d 100644
--- a/main.go
+++ b/main.go
@@ -137,7 +137,6 @@ func addHandler(w http.ResponseWriter, r *http.Request) {
}
wrms.AddSong(song)
- wrms.Broadcast(Event{"add", []Song{song}})
fmt.Fprintf(w, "Added song %s", string(data))
}
diff --git a/player.go b/player.go
index f8f6272..e7d1dd9 100644
--- a/player.go
+++ b/player.go
@@ -68,13 +68,11 @@ func (player *Player) runMpv() {
wrms := player.wrms
- llog.Info("mpv finished. Resetting mpv, the current song and call play")
+ llog.Info("mpv finished. Resetting mpv, and calling next")
player.mpv = nil
- wrms.Playing = false
player.Backends[wrms.CurrentSong.Source].OnSongFinished(&wrms.CurrentSong)
- wrms.CurrentSong.Uri = ""
- wrms.PlayPause()
+ wrms.Next()
}
func (player *Player) PlayUri(uri string) {
diff --git a/upload.go b/upload.go
index b3778ec..f3e8e52 100644
--- a/upload.go
+++ b/upload.go
@@ -66,7 +66,6 @@ func (b *UploadBackend) upload(w http.ResponseWriter, r *http.Request) {
}
wrms.AddSong(song)
- wrms.Broadcast(Event{"add", []Song{song}})
fmt.Fprintf(w, "Added uploaded song %s", string(fileName))
}
diff --git a/web/client.html b/web/client.html
index 77a4bfe..a4818af 100644
--- a/web/client.html
+++ b/web/client.html
@@ -170,7 +170,6 @@
}
function handleStop() {
- {{if .IsAdmin}} document.getElementById("ppbutton").innerHTML = 'Play'; {{end}}
playing = document.getElementById("playing").innerHTML = "";
}
diff --git a/wrms.go b/wrms.go
index 51e39f7..6696bb2 100644
--- a/wrms.go
+++ b/wrms.go
@@ -99,11 +99,22 @@ func (wrms *Wrms) Broadcast(ev Event) {
}
}
+func (wrms *Wrms) startPlaying() {
+ wrms.Broadcast(Event{"play", []Song{wrms.CurrentSong}})
+ wrms.Player.Play(&wrms.CurrentSong)
+}
+
func (wrms *Wrms) AddSong(song Song) {
+ startPlayingAgain := wrms.Playing && wrms.CurrentSong.Uri == ""
wrms.Songs = append(wrms.Songs, song)
s := &wrms.Songs[len(wrms.Songs)-1]
wrms.queue.Add(s)
llog.Info("Added song %s (ptr=%p) to Songs", s.Uri, s)
+ wrms.Broadcast(Event{"add", []Song{song}})
+
+ if startPlayingAgain {
+ wrms.Next()
+ }
}
func (wrms *Wrms) Next() {
@@ -112,6 +123,7 @@ func (wrms *Wrms) Next() {
next := wrms.queue.PopSong()
if next == nil {
wrms.CurrentSong.Uri = ""
+ wrms.Broadcast(newNotification("stop"))
return
}
@@ -126,30 +138,25 @@ func (wrms *Wrms) Next() {
}
wrms.CurrentSong = *next
+ if wrms.Playing {
+ wrms.startPlaying()
+ }
}
func (wrms *Wrms) PlayPause() {
- var ev Event
- if !wrms.Playing {
+ wrms.Playing = !wrms.Playing
+
+ if wrms.Playing {
if wrms.CurrentSong.Uri == "" {
llog.Info("No song currently playing play the next")
wrms.Next()
- if wrms.CurrentSong.Uri == "" {
- llog.Info("No song left to play")
- wrms.Broadcast(newNotification("stop"))
- return
- }
+ } else {
+ wrms.startPlaying()
}
- ev = Event{"play", []Song{wrms.CurrentSong}}
- wrms.Player.Play(&wrms.CurrentSong)
} else {
- ev = newNotification("pause")
+ wrms.Broadcast(newNotification("pause"))
wrms.Player.Pause()
}
-
- wrms.Playing = !wrms.Playing
-
- wrms.Broadcast(ev)
}
func (wrms *Wrms) AdjustSongWeight(connId uuid.UUID, songUri string, vote string) {