从Spotify获取并播放30秒预览

时间:2016-06-23 15:19:40

标签: android spotify

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

我需要能够搜索艺术家并播放弹出的第一个预览,而无需用户对其帐户进行身份验证

3 个答案:

答案 0 :(得分:0)

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

"搜索艺术家(和查找)演示"

这是网络代码段的一个示例,但您可以尝试查看连接的方式,而无需用户身份验证。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

因此,为了实现这一点,我必须将2个API调用链接在一起。第一个搜索艺术家,然后第二个搜索预览:

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

然后返回一个像这样的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
    }
    }

因为这会返回艺术家ID,我们可以获得该艺术家的顶级曲目并播放预览:

使用此URL(GET)调用API:

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

然后我们可以从这里提取预览的URL并播放它,所有这些都无需验证!