mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-27 00:25:54 +03:00
[GH-ISSUE #685] Playing a song on Spotify when not previously playing #404
Labels
No labels
api-bug
bug
dependencies
documentation
duplicate
enhancement
external-ide
headless-mode
implicit-grant-flow
invalid
missing-endpoint
pr-welcome
private-api
pull-request
question
spotipy3
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/spotipy#404
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @evanlaf on GitHub (May 29, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/685
Hi, I am using Windows 10 and Python 3.8 for Spotipy. I already validated the user connection, and found that I could play a song only if spotify is already running. For example, I can change the song that currently plays on my phone with the code below. However, my goal is to play a song when Spotify is not already playing, e.g., start Spotify and play a song. If Spotify is not already running, I receive an error message (also shown below). This can be solved by directly connecting with the Spotify web api like this, and I cannot find whether this is possible through spotipy; has anyone run into this issue and was able to solve it without directly talking to the web api?
Code:
Error when spotify is not already playing:
@Peter-Schorn commented on GitHub (May 29, 2021):
If you want to play content when no device is active, you must provide a device id to the
Spotify.start_playbackmethod. You can find the ids of the available devices using theSpotify.devicesendpoint.All of the Spotify web API endpoints, including the ones mentioned in the article you linked, are supported by spotipy:
https://developer.spotify.com/documentation/web-api/reference/player/transfer-a-users-playback/ ->
Spotify.transfer_playback.https://developer.spotify.com/documentation/web-api/reference/player/get-a-users-available-devices/ ->
Spotify.devices.@evanlaf commented on GitHub (May 30, 2021):
@Peter-Schorn Thanks for the comment. I looked into using those, and am still running into a similar issue. Either through their web console or in
spotipy(e.g.,sp.devices()), I can see the list of active devices and get the running device's id. Then, passing in the device id (e.g.,sp.start_playback(uris=['track'], device_id='device_id')) works if the device is active, but not if the device is inactive.So, passing in an inactive device's id does not allow Spotify to play on the local device. A workaround would be to always have a Spotify instance running on the local device, but as I want to use
spotipyon a Raspberry Pi, I don't think that is a valid solution. What are your thoughts?@Peter-Schorn commented on GitHub (May 30, 2021):
No, you can see the list of available devices and see which one (singular) is active by checking the
is_activeproperty of each device object in the list of devices.Do not confuse an available device with an active device. A device is available if it shows up in the list of devices returned by the
devicesmethod. In order for a device to be available, it must be open (e.g., the Spotify desktop application must be open for it to be considered available).A device becomes active when you pay content on it. When playback ceases, it typically becomes inactive only a few minutes later. A device immediately becomes inactive if you close the device or start playback on another device.
You can play content on any available device, whether it's active or not. If a device does not show up in the list of devices returned by the
devicesmethod, then it's considered unavailable and you cannot play content on it.You must have a spotify client (e.g, the desktop application) open in order to play content. The web API will not open a client for you. That's not how it works.
I'm not sure I understand the relevance of this part. Are you running the Spotify desktop application/ web player on the raspberry pi? If you're merely communicating with the web API programmatically using
spotipy, then the hardware you're using is irrelevant.@evanlaf commented on GitHub (May 30, 2021):
Ah yes I was confusing that terminology, thanks for the clarification. I did additional research and found that you can make a device a Spotify connect device, then play music off of that. This is especially useful in terms on running a device like the rpi. The reason I mentioned this is because of limited memory capabilities on the rpi and I don't see the need to have an application running 24/7 if it's only being time to time.
This was my original question, which makes sense, but was not clarified anywhere. I will most likely write a script that opens the Spotify application prior to playing music through
spotipy(and closes Spotify afterwards) due to the API's limited capabilities. Thanks for the clarifications.@olsonap commented on GitHub (Oct 28, 2021):
The way I handled this was
Then, you'd have to initiate playback somewhere using