From 039881fdbfa69a33b27f84e8db3671fe8cfac5e6 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 31 May 2023 22:06:53 +0200 Subject: support skipping the current song while playback is paused --- player.go | 33 +++++++++++++++++++++++---------- web/client.html | 9 ++++++--- wrms.go | 17 +++++++++++++---- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/player.go b/player.go index db4457c..2e1e7bf 100644 --- a/player.go +++ b/player.go @@ -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) diff --git a/wrms.go b/wrms.go index fd1d10f..9409b49 100644 --- a/wrms.go +++ b/wrms.go @@ -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")) -- cgit v1.2.3