-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpd.php
296 lines (227 loc) · 5.61 KB
/
mpd.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
include 'status.php';
include 'stats.php';
class mpd {
private $host = '';
private $port = '';
private $sock;
private $error = FALSE;
private $errorDescription = '';
private $connected = FALSE;
private $protocolVersion = '';
function __construct($host, $port) {
$this->host = $host;
$this->port = $port;
}
function isConnected() {
return $this->connected;
}
function getErrorDescription() {
return $this->errorDescription;
}
function getProtocolVersion() {
return $this->protocolVersion;
}
/*
* Connects to a mpd server given in $host at port $port.
* Returns TRUE if the connection was successfull, otherwise FALSE
*/
function connect() {
if (!$this->connected) {
$this->sock = fsockopen($this->host, $this->port, $errno, $errstr, 10);
if (!$this->sock) {
$this->errorDescription = $errstr;
return FALSE;
}
while (!feof($this->sock)) {
$response = fgets($this->sock, 1024);
if (strncmp('OK', $response, 2) == 0) {
$this->protocolVersion = explode('MPD ', $response)[1];
$this->connected = TRUE;
return TRUE;
}
return FALSE;
}
return FALSE;
}
return TRUE;
}
/*
* Closes the current connection to the mpd server.
*/
function disconnect() {
if ($this->connected) {
fclose($this->sock);
$this->connected = FALSE;
return TRUE;
}
return FALSE;
}
/*
* Sends the command given in $command to the mpd server.
*/
private function sendCommand($command) {
if (!$this->connected) {
echo 'Not connected!';
return NULL;
}
fputs($this->sock, "$command\n");
$resp = "";
// only set to false if no error occured
$this->error = TRUE;
while (!feof($this->sock)) {
$response = fgets($this->sock, 1024);
if (strncmp('OK', $response, 2) == 0) {
$this->error = FALSE;
break;
}
if (strncmp('ACK', $response, 3) == 0) {
$this->error = TRUE;
return NULL;
}
$resp .= $response;
}
return $resp;
}
/*
* Returns an array containing the playlists stored in /var/lib/mpd/playlists
*/
function listPlaylists() {
$response = $this->sendCommand('listplaylists');
if ($this->error) return NULL;
$playlists = explode('playlist', $response);
array_shift($playlists);
for ($i = 0; $i < count($playlists); $i++) {
$tempStr = $playlists[$i];
$tempStr = substr($tempStr, 2);
$tempStr = strtok($tempStr, chr(10));
$playlists[$i] = $tempStr;
}
return $playlists;
}
/*
* Returns an array containing the songs in the given playlist.
*/
function listPlaylist($playlist) {
$response = $this->sendCommand("listplaylist $playlist");
if ($this->error) return NULL;
$songs = explode('file: ', $response);
array_shift($songs);
for ($i = 0; $i < count($songs); $i++) {
$song = $songs[$i];
$songs[$i] = substr($song, 0, strlen($song)-1);
}
return $songs;
}
/*
* Loads the playlist given in $playlist.
*/
function loadPlaylist($playlist) {
$this->sendCommand("load $playlist");
}
/*
* Begins playing the playlist at song number $nr.
*/
function play($nr) {
$this->sendCommand("play $nr");
}
function isPlaying() {
$state = $this->getStatus()->getState();
if (strcmp($state, 'play') == 0) return TRUE;
return FALSE;
}
/*
* Plays the next song in the playlist.
*/
function next() {
$this->sendCommand('next');
}
function previous() {
$this->sendCommand('previous');
}
function pause() {
$this->sendCommand('pause 1');
}
function resume() {
$this->sendCommand('pause 0');
}
/*
* Stops playing.
*/
function stop() {
$this->sendCommand('stop');
}
/*
* Clears the current playlist
*/
function clear() {
$this->sendCommand('clear');
}
/*
* Sets the volume of mpd given in $vol.
*/
function setVolume($vol) {
if ($vol < 0 || $vol > 100) return false;
$this->sendCommand("setvol $vol");
return true;
}
/*
* Returns a status object out of the status response
*/
function getStatus() {
$response = $this->sendCommand('status');
$status = new status($response);
return $status;
}
/*
* Returns a stats object out of the stats response
*/
function getStats() {
$response = $this->sendCommand('stats');
$stats = new stats($response);
return $stats;
}
/*
* Returns the current song
*/
function getCurrentSong() {
$response = $this->sendCommand('currentsong');
if ($this->error) return NULL;
/*
* Parsing sometimes is wrong for web radio stations
*/
$indexStart = strpos($response, "Title:") + strlen("Title:");
$indexLenght = strpos($response, "Album:") - $indexStart;
$song = substr($response, $indexStart, $indexLenght);
return $song;
}
/*
* Returns all artist names
*/
function getArtistnames() {
$response = $this->sendCommand('list artist');
if ($this->error) return NULL;
$artists = explode('Artist: ', $response);
for ($i = 0; $i < count($artists); $i++) {
$tempStr = $artists[$i];
$tempStr = substr($tempStr, 0, -1);
$artists[$i] = $tempStr;
}
return $artists;
}
/*
* Returns all album names for a given artist
*/
function getAlbumnamesFromArtist($artist) {
$response = $this->sendCommand('list album "'.$artist.'"');
if ($this->error) return NULL;
$albums = explode('Album: ', $response);
for ($i = 0; $i < count($albums); $i++) {
$tempStr = $albums[$i];
$tempStr = substr($tempStr, 0, -1);
$albums[$i] = $tempStr;
}
return $albums;
}
}
?>