I've been playing around with Spotify API in Python (Spotipy) and I don't know how to get the duration of the track I'm currently playing on Spotify. I'm assuming it would look something like this:
global spotifyObject
trackInfo = spotifyObject.current_user_playing_track()
// would probably look something like this?
trackInfo['duration']
I suppose it would be a dictionary because trackInfo['currently_playing_type'] == 'ad'
worked successfully. After taking some time searching through the Spotipy documentation and guessing a few keywords, I still did not hit the bullseye.
In Android Java Spotify API, it was actually pretty straight forward:
mSpotifyAppRemote.getPlayerApi()
.subscribeToPlayerState()
.setEventCallback(playerState -> {
final Track track = playerState.track;
String trackName = track.name;
// here!
Long trackDuration = track.duration;
});
Any help is appreciated :)
Thank you!
current_user_playing_track
gets in response a json like this:
{
[...] # [...] means i've deleted a portion to keep the code short
"progress_ms": 5465,
"item": {
"album": [...]
"artists": [...]
"disc_number": 1,
"duration_ms": 163294,
"explicit": false
},
},
"currently_playing_type": "track",
"actions": {
"disallows": {
"pausing": true,
"skipping_prev": true
}
},
"is_playing": false
}
as we can see "duration_ms"
is inside "item"
, so to access it you need to do
trackInfo['item']["duration_ms"]
The spotify developer console might be helpful for cases like this: https://developer.spotify.com/console/get-users-currently-playing-track/