package mpd import ( "log" "time" "github.com/fhs/gompd/v2/mpd" ) const ( // List of subsystems: https://mpd.readthedocs.io/en/latest/protocol.html#querying-mpd-s-status SubSystemPlayer = "player" ) type Player struct { Watcher *mpd.Watcher Client *mpd.Client } func NewPlayer(net string, addr string) (*Player, error) { var ( p Player err error ) // We are only subscribing to the player subsystem p.Watcher, err = mpd.NewWatcher(net, addr, "", SubSystemPlayer) if err != nil { log.Fatalf("failed to create a watcher: %v", err) } p.Client, err = mpd.Dial(net, addr) if err != nil { log.Fatalf("failed to start mpd client: %v", err) } go func() { for range time.Tick(30 * time.Second) { p.Client.Ping() } }() return &p, nil } func (p *Player) Close() error { if err := p.Watcher.Close(); err != nil { return err } if err := p.Client.Close(); err != nil { return err } return nil }