从 Spotify 获取和播放 30 秒预览

Getting and playing 30 second previews from Spotify

我研究了如何播放 30 秒预览,但到目前为止我能找到的只是 android 需要用户身份验证的网络包装器 API。

我需要能够搜索艺术家并播放弹出的第一个预览,而无需用户验证他们的帐户

你应该检查:https://developer.spotify.com/web-api/code-examples/

"Search for an Artist (and Lookup) Demo"

这是一个网页片段示例,但您可以尝试查看它是如何在不需要用户身份验证的情况下进行连接的。

如果你看 spotify api webpage 他们提到 Endpoints marked “OAuth” above require application registration and user authorization via the Spotify Accounts Service to access certain data.

因此,为了让这个工作正常,我必须将 2 个 API 调用链接在一起。第一个搜索艺术家,第二个搜索预览:

您使用此 link (GET) 调用 API: https://api.spotify.com/v1/search?q=<artist name>&type=artist

然后 returns 一个像这样的 json 结构:

{
    "artists": {
    "href": "https://api.spotify.com/v1/search?query=bring+me+the+horizon&offset=0&limit=20&type=artist",
    "items": [
    {
    "external_urls": {
    "spotify": "https://open.spotify.com/artist/1Ffb6ejR6Fe5IamqA5oRUF"
    },
    "followers": {
    "href": null,
    "total": 1067846
    },
    "genres": [
    "metalcore"
    ],
    "href": "https://api.spotify.com/v1/artists/1Ffb6ejR6Fe5IamqA5oRUF",
    "id": "1Ffb6ejR6Fe5IamqA5oRUF",
    "images": [
    {
    "height": 640,
    "url": "https://i.scdn.co/image/49aad7da4f872acb3005727392631dab282423d1",
    "width": 640
    },
    {
    "height": 320,
    "url": "https://i.scdn.co/image/d9cf89b9db73b95ed15d9e29e30d0dd8afea23e2",
    "width": 320
    },
    {
    "height": 160,
    "url": "https://i.scdn.co/image/d9e514e15f4940c77029ef3b11291d557b345ae9",
    "width": 160
    }
    ],
    "name": "Bring Me The Horizon",
    "popularity": 76,
    "type": "artist",
    "uri": "spotify:artist:1Ffb6ejR6Fe5IamqA5oRUF"
    }
    ],
    "limit": 20,
    "next": null,
    "offset": 0,
    "previous": null,
    "total": 1
    }
    }

因为这 returns 艺术家 ID,我们可以获得该艺术家的热门曲目并播放预览:

用这个 URL (GET) 调用 API:

https://api.spotify.com/v1/artists/<ARTIST ID>/top-tracks?country=GB

然后我们可以从这里提取预览的 URL 并播放,完全无需身份验证!