From 70279c4c62ae4180706c0d27c5bbcf275c903100 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sun, 21 May 2023 21:24:27 +0200 Subject: 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. --- TODO.md | 2 +- main.go | 1 - player.go | 6 ++---- upload.go | 1 - web/client.html | 1 - wrms.go | 35 +++++++++++++++++++++-------------- 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) { -- cgit v1.2.3