本文整理匯總了Golang中github.com/fhs/gompd/mpd.Client.CurrentSong方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.CurrentSong方法的具體用法?Golang Client.CurrentSong怎麽用?Golang Client.CurrentSong使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/fhs/gompd/mpd.Client
的用法示例。
在下文中一共展示了Client.CurrentSong方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: currentSong
func currentSong(_ *Cmd, conn *mpd.Client) (r *Result, err error) {
info, err := conn.CurrentSong()
if err == nil {
r = NewResult("CurrentSong", info)
}
return
}
示例2: Render
func (m MPDInstance) Render() (item modules.Item) {
mpdItem := MPDItem{Name: m.name, Markup: "pango"}
mpdFormatData := MPDFormatData{}
var client *go_mpd.Client
if c, err := go_mpd.Dial("tcp", m.host_name+":"+strconv.Itoa(m.port)); err != nil {
log.Error(err.Error())
return nil
} else {
client = c
}
defer func(client *go_mpd.Client) {
if err := client.Close(); err != nil {
log.Error("Failed to disconnect: " + err.Error())
}
}(client)
if attrs, err := client.Status(); err == nil {
if state, ok := attrs["state"]; ok {
mpdFormatData.State = state
} else {
log.Error("Failed to read state.")
return nil
}
} else {
log.Error("Failed to obtain status: " + err.Error())
return nil
}
if attrs, err := client.CurrentSong(); err != nil {
log.Error("Failed to obtain current song: " + err.Error())
return nil
} else {
obj := reflect.ValueOf(&mpdFormatData).Elem()
for key, val := range attrs {
if f := obj.FieldByName(key); f.IsValid() {
val = strings.TrimSpace(val)
f.Set(reflect.ValueOf(val))
}
}
}
buffer := bytes.Buffer{}
if err := m.template.Execute(&buffer, mpdFormatData); err != nil {
log.Error("Failed to render mpd template: " + err.Error())
return nil
} else {
mpdItem.Text = buffer.String()
}
item = mpdItem
return
}
示例3: StatusUpdater
// StatusUpdater is triggerd on "player" events and fetches the current state infos
// needed for the moodbar sync. It then proceeds to generate a MPDEvent that
// MoodbarAdjuster will receive.
func StatusUpdater(client *mpd.Client, cfg *Config, updateCh <-chan bool, eventCh chan<- MPDEvent) {
lastSongID := ""
for range updateCh {
song, err := client.CurrentSong()
if err != nil {
log.Printf("Unable to fetch current song: %v", err)
continue
}
status, err := client.Status()
if err != nil {
log.Printf("Unable to fetch status: %v", err)
continue
}
// Check if the song changed compared to last time:
// (always true for the first iteration)
songChanged := false
if status["songid"] != lastSongID {
lastSongID = status["songid"]
songChanged = true
}
// Find out how much progress we did in the current song.
// These atteributes might be empty for the stopped state.
elapsedMs, err := strconv.ParseFloat(status["elapsed"], 64)
if err != nil && status["elapsed"] != "" {
log.Printf("Failed to parse elpased (%s): %v", status["elapsed"], err)
}
elapsedMs *= 1000
totalMs, err := strconv.Atoi(song["Time"])
if err != nil && song["Time"] != "" {
log.Printf("Failed to parse total (%s): %v", song["time"], err)
}
totalMs *= 1000
// Find out if some music is playing...
isPlaying, isStopped := false, false
switch status["state"] {
case "play":
isPlaying = true
case "stop":
isStopped = true
}
// Send the appropiate event:
eventCh <- MPDEvent{
Path: filepath.Join(
cfg.MoodDir,
strings.Replace(song["file"], string(filepath.Separator), "|", -1),
),
SongChanged: songChanged,
ElapsedMs: elapsedMs,
TotalMs: float64(totalMs),
IsPlaying: isPlaying,
IsStopped: isStopped,
}
}
}