-
Notifications
You must be signed in to change notification settings - Fork 14
/
client.go
186 lines (156 loc) · 5.04 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package mpv
import (
"errors"
"fmt"
"strconv"
)
// Client is a more comfortable higher level interface
// to LLClient. It can use any LLClient implementation.
type Client struct {
LLClient
}
// NewClient creates a new highlevel client based on a lowlevel client.
func NewClient(llclient LLClient) *Client {
return &Client{
llclient,
}
}
// Mode options for Loadfile
const (
LoadFileModeReplace = "replace"
LoadFileModeAppend = "append"
LoadFileModeAppendPlay = "append-play" // Starts if nothing is playing
)
// Loadfile loads a file, it either replaces the currently playing file (LoadFileModeReplace),
// appends to the current playlist (LoadFileModeAppend) or appends to playlist and plays if
// nothing is playing right now (LoadFileModeAppendPlay)
func (c *Client) Loadfile(path string, mode string) error {
_, err := c.Exec("loadfile", path, mode)
return err
}
// Mode options for Seek
const (
SeekModeRelative = "relative"
SeekModeAbsolute = "absolute"
)
// Seek seeks to a position in the current file.
// Use mode to seek relative to current position (SeekModeRelative) or absolute (SeekModeAbsolute).
func (c *Client) Seek(n int, mode string) error {
_, err := c.Exec("seek", strconv.Itoa(n), mode)
return err
}
// PlaylistNext plays the next playlistitem or NOP if no item is available.
func (c *Client) PlaylistNext() error {
_, err := c.Exec("playlist-next", "weak")
return err
}
// PlaylistPrevious plays the previous playlistitem or NOP if no item is available.
func (c *Client) PlaylistPrevious() error {
_, err := c.Exec("playlist-prev", "weak")
return err
}
// Mode options for LoadList
const (
LoadListModeReplace = "replace"
LoadListModeAppend = "append"
)
// LoadList loads a playlist from path. It can either replace the current playlist (LoadListModeReplace)
// or append to the current playlist (LoadListModeAppend).
func (c *Client) LoadList(path string, mode string) error {
_, err := c.Exec("loadlist", path, mode)
return err
}
// GetProperty reads a property by name and returns the data as a string.
func (c *Client) GetProperty(name string) (string, error) {
res, err := c.Exec("get_property", name)
if res == nil {
return "", err
}
return fmt.Sprintf("%#v", res.Data), err
}
// SetProperty sets the value of a property.
func (c *Client) SetProperty(name string, value interface{}) error {
_, err := c.Exec("set_property", name, value)
return err
}
// ErrInvalidType is returned if the response data does not match the methods return type.
// Use GetProperty or find matching type in mpv docs.
var ErrInvalidType = errors.New("Invalid type")
// GetFloatProperty reads a float property and returns the data as a float64.
func (c *Client) GetFloatProperty(name string) (float64, error) {
res, err := c.Exec("get_property", name)
if res == nil {
return 0, err
}
if val, found := res.Data.(float64); found {
return val, err
}
return 0, ErrInvalidType
}
// GetBoolProperty reads a bool property and returns the data as a boolean.
func (c *Client) GetBoolProperty(name string) (bool, error) {
res, err := c.Exec("get_property", name)
if res == nil {
return false, err
}
if val, found := res.Data.(bool); found {
return val, err
}
return false, ErrInvalidType
}
// Filename returns the currently playing filename
func (c *Client) Filename() (string, error) {
return c.GetProperty("filename")
}
// Path returns the currently playing path
func (c *Client) Path() (string, error) {
return c.GetProperty("path")
}
// Pause returns true if the player is paused
func (c *Client) Pause() (bool, error) {
return c.GetBoolProperty("pause")
}
// SetPause pauses or unpauses the player
func (c *Client) SetPause(pause bool) error {
return c.SetProperty("pause", pause)
}
// Idle returns true if the player is idle
func (c *Client) Idle() (bool, error) {
return c.GetBoolProperty("idle")
}
// Mute returns true if the player is muted.
func (c *Client) Mute() (bool, error) {
return c.GetBoolProperty("mute")
}
// SetMute mutes or unmutes the player.
func (c *Client) SetMute(mute bool) error {
return c.SetProperty("mute", mute)
}
// Fullscreen returns true if the player is in fullscreen mode.
func (c *Client) Fullscreen() (bool, error) {
return c.GetBoolProperty("fullscreen")
}
// SetFullscreen activates/deactivates the fullscreen mode.
func (c *Client) SetFullscreen(v bool) error {
return c.SetProperty("fullscreen", v)
}
// Volume returns the current volume level.
func (c *Client) Volume() (float64, error) {
return c.GetFloatProperty("volume")
}
// Speed returns the current playback speed.
func (c *Client) Speed() (float64, error) {
return c.GetFloatProperty("speed")
}
// Duration returns the duration of the currently playing file.
func (c *Client) Duration() (float64, error) {
return c.GetFloatProperty("duration")
}
// Position returns the current playback position in seconds.
func (c *Client) Position() (float64, error) {
return c.GetFloatProperty("time-pos")
}
// PercentPosition returns the current playback position in percent.
func (c *Client) PercentPosition() (float64, error) {
return c.GetFloatProperty("percent-pos")
}