spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
5.01k stars 953 forks source link

Is it possible to tell Spotify to play to my chromecast using Spotipy? #750

Closed Marterido closed 4 months ago

Marterido commented 2 years ago

I'm currently making a voice assistant in python to control different things inside my house including music. But I wondered if it's possible to stream my Spotify to the chromecast or another device using Spotipy?

tikeyknax commented 2 years ago

I don't know about chromecast but I'm using it to stream spotify to Yamaha MusicCast devices. You can check which devices are available with the following code:

from pprint import pprint

import spotipy
from spotipy.oauth2 import SpotifyOAuth

scope = "user-read-playback-state,user-modify-playback-state"
sp = spotipy.Spotify(client_credentials_manager=SpotifyOAuth(scope=scope))
print(sp.me())

def show_devices():
    # Shows playing devices
    res = sp.devices()
    pprint(res)
    for d in res["devices"]:
        print(f"{d['name']}: {d['id']}")

    return res["devices"][0]["id"]

if __name__ == '__main__':
    d_id = show_devices()

    sp.start_playback(
        device_id=d_id,
        uris=["spotify:track:2R6UrJ8uWbSIiHWmvRQvN8"])

EDIT: Added the start_playback command with which the streaming to a device can actually be started.

dieser-niko commented 4 months ago

Closing as the issue seems to be resolved.