Skip to content

Instantly share code, notes, and snippets.

@bdunnette
Last active August 5, 2021 14:56
Show Gist options
  • Save bdunnette/2c9ee84691044f8c55d074ebb4f3f637 to your computer and use it in GitHub Desktop.
Save bdunnette/2c9ee84691044f8c55d074ebb4f3f637 to your computer and use it in GitHub Desktop.
Mopidy script to (shuffle) play tracks from a specified playlist
{
"name": "mopidy-play",
"version": "1.0.0",
"description": "",
"main": "play-mopidy-playlist.js",
"scripts": {
"start": "node play-mopidy-playlist.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Brian Dunnette",
"license": "MIT",
"dependencies": {
"mopidy": "^1.3.0"
}
}
const Mopidy = require("mopidy");
// Change this to the uri/ID of the playlist you want to use
const playlistUri = "spotify:playlist:37i9dQZF1EQmPV0vrce2QZ"
const mopidy = new Mopidy({
webSocketUrl: "ws://localhost:6680/mopidy/ws/",
autoConnect: false
});
mopidy.on("state", console.log);
mopidy.on("event", console.log);
mopidy.connect();
// Wait for the connection to be established
mopidy.on("state:online", () => {
// Clear tracklist
mopidy.tracklist.clear().then(() => {
// Get tracks from playlist
mopidy.playlists.getItems({ uri: playlistUri }).then(items => {
// Add playlist uris to tracklist
mopidy.tracklist.add({ uris: items.map(item => item.uri) }).then(() => {
// Shuffle tracklist
mopidy.tracklist.shuffle().then(() => {
// Start playing
mopidy.playback.play().then(() => {
// Once playback starts, exit
mopidy.on("event:trackPlaybackStarted", () => {
process.exit(0);
});
});
});
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment