diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-05-31 22:06:53 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2023-05-31 22:06:53 +0200 |
| commit | 039881fdbfa69a33b27f84e8db3671fe8cfac5e6 (patch) | |
| tree | 3746f6394f7999f1b6bf8826c9a364c11febc6ab | |
| parent | 0d0b416146dd146a5dcfbf61f09f8136aa5afdc2 (diff) | |
| download | WRMS-039881fdbfa69a33b27f84e8db3671fe8cfac5e6.tar.gz WRMS-039881fdbfa69a33b27f84e8db3671fe8cfac5e6.zip | |
support skipping the current song while playback is paused
| -rw-r--r-- | player.go | 33 | ||||
| -rw-r--r-- | web/client.html | 9 | ||||
| -rw-r--r-- | wrms.go | 17 |
3 files changed, 42 insertions, 17 deletions
@@ -48,15 +48,6 @@ func NewPlayer(wrms *Wrms, backends []string) Player { } func (player *Player) Play(song *Song) { - if player.mpv != nil { - llog.Debug("Send SIGCONT to mpv subprocess") - err := player.mpv.Process.Signal(syscall.SIGCONT) - if err != nil { - llog.Fatal("Failed to send SIGCONT to mpv") - } - return - } - llog.Info("Start playing %v", song) player.Backends[song.Source].Play(song, player) } @@ -65,7 +56,7 @@ func (player *Player) runMpv() { output, err := player.mpv.CombinedOutput() if err != nil { llog.Debug("Mpv output: %s", output) - llog.Fatal("Mpv failed with: %s", err.Error()) + llog.Fatal("Mpv failed with: %s", err) } currentSong := wrms.CurrentSong.Load() @@ -77,6 +68,15 @@ func (player *Player) runMpv() { wrms.Next() } +func (player *Player) terminateMpv() { + if player.mpv == nil { + llog.Fatal("Trying to terminate not running mpv process") + } + + player.mpv.Process.Signal(syscall.SIGTERM) + player.mpv = nil +} + const MPV_FLAGS = "--no-video" func mpvArgv(uri string) []string { @@ -133,6 +133,19 @@ func (player *Player) Pause() { } } +func (player *Player) Continue() { + if player.mpv == nil { + llog.Warning("Continue Play but there is no running mpv process to continue") + return + } + + llog.Debug("Send SIGCONT to mpv subprocess") + err := player.mpv.Process.Signal(syscall.SIGCONT) + if err != nil { + llog.Fatal("Failed to send SIGCONT to mpv") + } +} + func (player *Player) Search(pattern string) chan []Song { var wg sync.WaitGroup wg.Add(len(player.Backends)) diff --git a/web/client.html b/web/client.html index c0aef1f..8e0fa04 100644 --- a/web/client.html +++ b/web/client.html @@ -202,8 +202,8 @@ playing = document.getElementById("playing").innerHTML = ""; } - function handlePlay(_currentSongs) { - {{if .IsAdmin}} document.getElementById("ppbutton").innerHTML = 'Pause'; {{end}} + function handlePlayOrNext(cmd, _currentSongs) { + {{if .IsAdmin}}if (cmd == "play") { document.getElementById("ppbutton").innerHTML = 'Pause'; }{{end}} if (_currentSongs == null || _currentSongs.length == 0) { return; @@ -257,7 +257,10 @@ handleStop() break; case "play": - handlePlay(cmd.songs) + handlePlayOrNext("play", cmd.songs) + break; + case "next": + handlePlayOrNext("next", cmd.songs) break; case "upvoted": handleVotes("up", cmd.songs) @@ -151,7 +151,6 @@ func (wrms *Wrms) Broadcast(ev Event) { func (wrms *Wrms) startPlaying() { currentSong := wrms.CurrentSong.Load() wrms.Broadcast(newEvent("play", []Song{*currentSong})) - wrms.Player.Play(currentSong) } func (wrms *Wrms) AddSong(song Song) { @@ -206,7 +205,11 @@ func (wrms *Wrms) Next() { } if wrms.Playing { - wrms.startPlaying() + wrms.Broadcast(newEvent("play", []Song{*next})) + wrms.Player.Play(next) + } else if wrms.Player.mpv != nil { + wrms.Broadcast(newEvent("next", []Song{*next})) + wrms.Player.terminateMpv() } } @@ -214,11 +217,17 @@ func (wrms *Wrms) PlayPause() { wrms.Playing = !wrms.Playing if wrms.Playing { - if wrms.CurrentSong.Load() == nil { + currentSong := wrms.CurrentSong.Load() + if currentSong == nil { llog.Info("No song currently playing play the next") wrms.Next() } else { - wrms.startPlaying() + wrms.Broadcast(newEvent("play", []Song{*currentSong})) + if wrms.Player.mpv != nil { + wrms.Player.Continue() + } else { + wrms.Player.Play(currentSong) + } } } else { wrms.Broadcast(newNotification("pause")) |
