Skip to content

Commit 6341856

Browse files
author
Jesse Newland
committed
Add support for repeat and shuffle
1 parent dae2c7d commit 6341856

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ and what is playing.
7575
"name": "Forever",
7676
"artist": "HAIM",
7777
"album": "Days Are Gone (2013)",
78-
"playlist": "Summer Jams"
78+
"playlist": "Summer Jams",
79+
"shuffle": "songs",
80+
"repeat": "all"
7981
}
8082
```
8183

@@ -133,6 +135,10 @@ These are the endpoints you can hit to do things.
133135
PUT /pause => NowPlayingResource
134136
PUT /volume [level=20] => NowPlayingResource
135137
PUT /volume [muted=true] => NowPlayingResource
138+
PUT /shuffle [mode=songs] => NowPlayingResource
139+
PUT /shuffle [mode=off] => NowPlayingResource
140+
PUT /repeat [mode=all] => NowPlayingResource
141+
PUT /repeat [mode=off] => NowPlayingResource
136142

137143

138144
#### Playlists

app.js

+56
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function getCurrentState(){
3535
currentState['playlist'] = currentPlaylist.name();
3636
currentState['volume'] = itunes.soundVolume();
3737
currentState['muted'] = itunes.mute();
38+
currentState['repeat'] = itunes.songRepeat();
39+
currentState['shuffle'] = itunes.shuffleEnabled() && itunes.shuffleMode();
3840

3941
if (currentTrack.year()) {
4042
currentState['album'] += " (" + currentTrack.year() + ")";
@@ -95,6 +97,39 @@ function setMuted(muted){
9597
}
9698
}
9799

100+
function setShuffle(mode){
101+
itunes = Application('iTunes');
102+
103+
if (!mode) {
104+
mode = "songs"
105+
}
106+
107+
if (mode == "false" || mode == "off") {
108+
itunes.shuffleEnabled = false;
109+
return false;
110+
}else{
111+
itunes.shuffleEnabled = true;
112+
itunes.shuffleMode = mode;
113+
return true;
114+
}
115+
}
116+
117+
function setRepeat(mode){
118+
itunes = Application('iTunes');
119+
120+
if (!mode) {
121+
mode = "all"
122+
}
123+
124+
if (mode == "false" || mode == "off") {
125+
itunes.songRepeat = false;
126+
return false;
127+
}else{
128+
itunes.songRepeat = mode;
129+
return true;
130+
}
131+
}
132+
98133
function getPlaylistsFromItunes(){
99134
itunes = Application('iTunes');
100135
playlists = itunes.playlists();
@@ -195,6 +230,27 @@ app.put('/mute', function(req, res){
195230
})
196231
})
197232

233+
app.put('/shuffle', function(req, res){
234+
osa(setShuffle, req.body.mode, function(error, data, log){
235+
if (error){
236+
console.log(error)
237+
res.sendStatus(500)
238+
}else{
239+
sendResponse(error, res)
240+
}
241+
})
242+
})
243+
244+
app.put('/repeat', function(req, res){
245+
osa(setRepeat, req.body.mode, function(error, data, log){
246+
if (error){
247+
console.log(error)
248+
res.sendStatus(500)
249+
}else{
250+
sendResponse(error, res)
251+
}
252+
})
253+
})
198254

199255
app.get('/now_playing', function(req, res){
200256
error = null

public/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ <h2>Player Control</h2>
3737
<p><span class="method">PUT</span> /previous</p>
3838
<p><span class="method">PUT</span> /volume [level=20]</p>
3939
<p><span class="method">PUT</span> /mute [muted=true]</p>
40+
<p><span class="method">PUT</span> /shuffle [mode=songs]</p>
41+
<p><span class="method">PUT</span> /repeat [mode=all]</p>
4042

4143
<h2>Playlists</h2>
4244
<p><span class="method">GET</span> <a href="/playlists">/playlists</a></p>

0 commit comments

Comments
 (0)