Skip to content

Commit 30f6f17

Browse files
committed
add set player position api
1 parent 344d624 commit 30f6f17

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

api.js

+43
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ function rampVolume(volume) {
6767
});
6868
}
6969

70+
function setPlayerPosition(seconds) {
71+
let positionScript = `tell application "Spotify"
72+
set currentPosition to get player position
73+
set desiredPosition to (currentPosition + ${seconds})
74+
set player position to desiredPosition
75+
end tell`;
76+
77+
STATUS.playbackInfo.playerState = `Moving Player Position ${seconds} seconds`;
78+
79+
return osascript(positionScript).then(function(response) {
80+
return response;
81+
});
82+
}
83+
7084
module.exports = {
7185
start: function(port) {
7286
//starts the REST API
@@ -165,6 +179,21 @@ module.exports = {
165179
}
166180
});
167181

182+
server.get('/playerPosition/:seconds', function (req, res) {
183+
if (config.get('allowControl')) {
184+
try {
185+
setPlayerPosition(req.params.seconds);
186+
res.send({status: 'player-position-changed'});
187+
}
188+
catch(error) {
189+
res.send({error: error});
190+
}
191+
}
192+
else {
193+
res.send({status: 'not-allowed'});
194+
}
195+
});
196+
168197
server.get('/next', function (req, res) {
169198
if (config.get('allowControl')) {
170199
try {
@@ -437,6 +466,20 @@ module.exports = {
437466
}
438467
});
439468

469+
socket.on('playerPosition', function (seconds) {
470+
if (config.get('allowControl')) {
471+
try {
472+
setPlayerPosition(seconds);
473+
}
474+
catch(error) {
475+
socket.emit('error', error);
476+
}
477+
}
478+
else {
479+
socket.emit('control_status', false);
480+
}
481+
});
482+
440483
socket.on('playtrack', function (track) {
441484
if (config.get('allowControl')) {
442485
try {

api.md

+11
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ All requests are HTTP GET.
8383
{status: 'play-pause-toggled'}
8484
```
8585

86+
* `/playerPosition/[seconds]`:
87+
88+
Moves the player position of the currently playing track forward or backward, in seconds.
89+
```javascript
90+
{status: 'player-position-changed'}
91+
```
92+
8693
* `/next`:
8794

8895
Goes to next track.
@@ -261,6 +268,10 @@ Upon connection, the server will emit the `control_status` event to let the clie
261268

262269
Toggles playback between play/pause.
263270

271+
* `playerPosition`:
272+
273+
Moves the player position of the currently playing track forward or backward based on the first argument, in seconds.
274+
264275
* `next`:
265276

266277
Goes to next track.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "spotify-controller",
33
"productName": "Spotify Controller",
4-
"version": "0.1.8",
4+
"version": "0.1.9",
55
"description": "Control Spotify on MacOS over the network",
66
"license": "MIT",
77
"repository": "josephdadams/spotify-controller",

util.js

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ module.exports = {
3232
switch(event) {
3333
case 'com.spotify.client.PlaybackStateChanged':
3434
STATUS.playbackInfo = _.mapKeys(info, (v, k) => _.camelCase(k));
35-
console.log(STATUS);
3635
API.sendUpdates();
3736
showNotification();
3837
break;

0 commit comments

Comments
 (0)