Skip to content

Instantly share code, notes, and snippets.

@kopiro
Last active November 27, 2020 11:31
Show Gist options
  • Save kopiro/5325a559302e78d81739a132ff988652 to your computer and use it in GitHub Desktop.
Save kopiro/5325a559302e78d81739a132ff988652 to your computer and use it in GitHub Desktop.
Spotify CastV2 Client example
const Client = require('castv2-client').Client;
const Spotify = require('./Spotify');
const mdns = require('mdns');
const browser = mdns.createBrowser(mdns.tcp('googlecast'));
browser.on('serviceUp', function(service) {
console.log('found device "%s" at %s:%d', service.txtRecord.fn, service.addresses[0], service.port);
if (service.txtRecord.fn === process.env.SPOTFIY_DEVICE) {
ondeviceup(service.addresses[0], service.txtRecord.fn);
browser.stop();
}
});
browser.start();
function ondeviceup(host, device_name) {
var client = new Client();
client.connect(host, function() {
console.log('connected, launching Spotify app on ' + device_name + '...');
client.launch(Spotify, async(err, player) => {
await player.authenticate({
username: process.env.SPOTIFY_USERNAME,
password: process.env.SPOTIFY_PASSWORD,
device_name: process.env.SPOTIFY_DEVICE
});
console.log('Authentication OK');
const artist = (await player.getAPI().searchArtists('Arcade Fire')).body.artists.items;
await player.play({
context_uri: artist[0].uri
});
console.log('Play OK');
setTimeout(() => {
player.pause();
console.log('Pause OK');
}, 10 * 1000);
});
});
client.on('error', function(err) {
console.log('Error: %s', err.message);
client.close();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment