Skip to content

Commit

Permalink
[Spotify] Only allow expected track to play
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Jan 17, 2020
1 parent 8f82266 commit 6931295
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/plugins/spotify/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ export default {
play(uri?: string) {
return ensureAuthenticated()
.then(ensurePlayer)
.tap(() => log.debug(`spotify.play(${uri})`))
.then(() => {
if (uri) {
player.setExpectedURI(uri);
}
})
.then(() => (uri ? [uri] : undefined))
.then(uris => spotify.play({ uris }))
.then(() => player.waitForPlayback(uri))
Expand Down
13 changes: 13 additions & 0 deletions src/plugins/spotify/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ export default {
.then(el => el.getText());
},

setExpectedURI(id: string) {
if (!driver) {
return Promise.reject(new Error("No driver"));
}

let d: Driver = driver;
// Get the device ID
return Promise.resolve()
.then(() => d.findElement(By.id("expectedURI")))
.tap(e => e.clear())
.tap(e => e.sendKeys(id));
},

getStatus() {
if (!driver) {
return Promise.reject(new Error("No driver"));
Expand Down
8 changes: 8 additions & 0 deletions support/plugins/spotify/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<title>Spotify Web Player (loading)</title>
</head>
<body>
<input type="text" id="expectedURI" />

<span id="device_id"></span>
<span id="device_state">loading</span>

Expand Down Expand Up @@ -48,6 +50,12 @@
const track = state.track_window.current_track;

if (!state.paused) {
if (track.uri != expectedURI.value) {
console.log(
`MISMATCHED TRACKS -- Expected ${expectedURI.value}, got ${track.id}`
);
player.pause().then(() => player.seek(0));
}
document.getElementById("device_state").innerText = "playing";
} else {
document.getElementById("device_state").innerText = "paused";
Expand Down

0 comments on commit 6931295

Please sign in to comment.