diff --git a/mpvplayer/handler.go b/mpvplayer/handler.go index d00a6f1..a700877 100644 --- a/mpvplayer/handler.go +++ b/mpvplayer/handler.go @@ -25,33 +25,23 @@ func (p *Player) EventLoop() { } else if evt.Event_Id == mpv.EVENT_PROPERTY_CHANGE { // one of our observed properties changed. which one is probably extractable from evt.Data.. somehow. - position, err := p.instance.GetProperty("playback-time", mpv.FORMAT_INT64) + position, err := p.getPropertyInt64("playback-time") if err != nil { p.logger.Printf("mpv.EventLoop (%s): GetProperty %s -- %s", evt.Event_Id.String(), "playback-time", err.Error()) } - duration, err := p.instance.GetProperty("duration", mpv.FORMAT_INT64) + duration, err := p.getPropertyInt64("duration") if err != nil { p.logger.Printf("mpv.EventLoop (%s): GetProperty %s -- %s", evt.Event_Id.String(), "duration", err.Error()) } - volume, err := p.instance.GetProperty("volume", mpv.FORMAT_INT64) + volume, err := p.getPropertyInt64("volume") if err != nil { p.logger.Printf("mpv.EventLoop (%s): GetProperty %s -- %s", evt.Event_Id.String(), "volume", err.Error()) } - if position == nil { - position = int64(0) - } - if duration == nil { - duration = int64(0) - } - if volume == nil { - volume = int64(0) - } - statusData := StatusData{ - Volume: volume.(int64), - Position: position.(int64), - Duration: duration.(int64), + Volume: volume, + Position: position, + Duration: duration, } p.remoteState.timePos = float64(statusData.Position) p.sendGuiDataEvent(EventStatus, statusData) diff --git a/mpvplayer/helpers.go b/mpvplayer/helpers.go new file mode 100644 index 0000000..891e79f --- /dev/null +++ b/mpvplayer/helpers.go @@ -0,0 +1,27 @@ +package mpvplayer + +import ( + "errors" + + "github.com/spezifisch/go-mpv" +) + +func (p *Player) getPropertyInt64(name string) (int64, error) { + value, err := p.instance.GetProperty(name, mpv.FORMAT_INT64) + if err != nil { + return 0, err + } else if value == nil { + return 0, errors.New("nil value") + } + return value.(int64), err +} + +func (p *Player) getPropertyBool(name string) (bool, error) { + value, err := p.instance.GetProperty(name, mpv.FORMAT_FLAG) + if err != nil { + return false, err + } else if value == nil { + return false, errors.New("nil value") + } + return value.(bool), err +} diff --git a/mpvplayer/player.go b/mpvplayer/player.go index 644c63d..0c536bf 100644 --- a/mpvplayer/player.go +++ b/mpvplayer/player.go @@ -84,16 +84,6 @@ func (p *Player) mpvEngineEventHandler(instance *mpv.Mpv) { } } -func (p *Player) getPropertyInt64(name string) (int64, error) { - value, err := p.instance.GetProperty(name, mpv.FORMAT_INT64) - if err != nil { - return 0, err - } else if value == nil { - return 0, errors.New("nil value") - } - return value.(int64), err -} - func (p *Player) Quit() { p.mpvEvents <- nil p.instance.TerminateDestroy() @@ -156,26 +146,26 @@ func (p *Player) temporaryStop() error { } func (p *Player) IsSongLoaded() (bool, error) { - idle, err := p.instance.GetProperty("idle-active", mpv.FORMAT_FLAG) - return !idle.(bool), err + idle, err := p.getPropertyBool("idle-active") + return !idle, err } func (p *Player) IsPaused() (bool, error) { - pause, err := p.instance.GetProperty("pause", mpv.FORMAT_FLAG) - return pause.(bool), err + pause, err := p.getPropertyBool("pause") + return pause, err } func (p *Player) IsPlaying() (playing bool, err error) { - if idle, err := p.instance.GetProperty("idle-active", mpv.FORMAT_FLAG); err != nil { - } else if paused, err := p.instance.GetProperty("pause", mpv.FORMAT_FLAG); err != nil { + if idle, err := p.getPropertyBool("idle-active"); err != nil { + } else if paused, err := p.getPropertyBool("pause"); err != nil { } else { - playing = !idle.(bool) && !paused.(bool) + playing = !idle && !paused } return } func (p *Player) Test() { - res, err := p.instance.GetProperty("idle-active", mpv.FORMAT_FLAG) + res, err := p.getPropertyBool("idle-active") p.logger.Printf("res %v err %v", res, err) }