如何使用 Spotipy 获取 Spotify 的曲目持续时间

How to get Track Duration for Spotify using Spotipy

我一直在 Python (Spotipy) 中使用 Spotify API,但我不知道如何获取我当前在 Spotify 上播放的曲目的持续时间。 我假设它看起来像这样:

global spotifyObject
trackInfo = spotifyObject.current_user_playing_track()

// would probably look something like this?
trackInfo['duration']

我想这会是一本字典,因为 trackInfo['currently_playing_type'] == 'ad' 成功了。在花了一些时间搜索 Spotipy 文档并猜测了几个关键字后,我仍然没有命中靶心。

在 Android Java Spotify API 中,它实际上非常简单:

mSpotifyAppRemote.getPlayerApi()
            .subscribeToPlayerState()
            .setEventCallback(playerState -> {
                final Track track = playerState.track;
                String trackName = track.name;
                // here!
                Long trackDuration = track.duration;
                });

感谢任何帮助:)

谢谢!

current_user_playing_track 得到这样的 json 响应:

{
  [...] # [...] 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
}

我们可以看到 "duration_ms""item" 里面,所以要访问它你需要做

trackInfo['item']["duration_ms"]

Spotify 开发人员控制台可能对以下情况有所帮助:https://developer.spotify.com/console/get-users-currently-playing-track/