Compare commits

..

1 Commits

Author SHA1 Message Date
Noah 1fbd6bce2d Crontab installation support 2018-10-24 13:56:50 -07:00
4 changed files with 31 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import (
"path/filepath"
"strconv"
"strings"
"time"
)
// Player manages playing music one track at a time.
@ -17,6 +18,23 @@ type Player struct {
cmd *exec.Cmd
}
// Watch the running playlist to advance the next track.
func (s *Sonar) Watch() {
var p = s.Player
for {
if p.Running() {
if err := p.cmd.Wait(); err != nil {
log.Error("Player.Watch: p.cmd.Wait: %s", err)
}
// Next track?
log.Info("Track ended, play next")
s.PlayNext()
}
time.Sleep(1 * time.Second)
}
}
// Run the playlist.
func (p *Player) Run(playlist []string) {
p.playlist = playlist
@ -178,5 +196,6 @@ func (s *Sonar) PlayNext() error {
if !ok {
return errors.New("end of playlist")
}
s.Player.Stop()
return s.Play(filename)
}

View File

@ -52,6 +52,14 @@ func (s *Sonar) Register() *mux.Router {
s.FlashAndRedirect(r, w, "/", "Playlist stopped!")
}
})
POST.HandleFunc("/playlist/next", func(w http.ResponseWriter, r *http.Request) {
err := s.PlayNext()
if err != nil {
s.FlashAndRedirect(r, w, "/", "Error: "+err.Error())
} else {
s.FlashAndRedirect(r, w, "/", "Playlist advanced!")
}
})
POST.HandleFunc("/playlist/schedule", func(w http.ResponseWriter, r *http.Request) {
var (
timestamp = r.FormValue("time")

View File

@ -46,6 +46,9 @@ func New() *Sonar {
)
s.n.UseHandler(router)
// Watch the player to advance to the next track.
go s.Watch()
return s
}

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>time.caskir.net</title>
<title>Sonar</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="content-type" value="text/html; encoding=UTF-8">
<link rel="stylesheet" href="/css/bootstrap.min.css">