微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我一直在使用 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/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。