使用 Soundcloud 播放特定歌曲 API
Stream a specific song using Soundcloud API
我仍在学习如何使用 API - 这给我带来了麻烦。我相信我的代码是正确的,但是当我尝试使用简单的 JavaScript onclick 函数播放歌曲时没有任何反应。
HTML:
<head>
<script src="https://connect.soundcloud.com/sdk/sdk-3.0.0.js"></script>
<script src="good.js"></script>
</head>
<body>
<button onclick="playIt()">play adele</button>
</body>
JavaScript:
SC.initialize({
client_id: 'XXXXXXXXXX',
});
function playIt(){
var sound = SC.stream("/emberwaves/adele-set-fire-to-the-rain-remix", function(sound){
sound.play();
});
}
首先,使用 resolve
端点获取该轨道的 API URL:
HTTP GET: https://api.soundcloud.com/resolve.json?url=https://soundcloud.com/emberwaves/adele-set-fire-to-the-rain-remix&client_id=[CLIENT_ID]
回复:
HTTP/1.1 302 Found
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Origin
Access-Control-Allow-Methods: GET, PUT, POST, DELETE
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Date
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Date: Thu, 07 Jan 2016 19:54:36 GMT
Location: https://api.soundcloud.com/tracks/43377447.json?client_id=[CLIENT_ID]
Server: am/2
Status: 302 Found
Content-Length: 128
Connection: close
{"status":"302 - Found","location":"https://api.soundcloud.com/tracks/43377447.json?client_id=[CLIENT_ID]"}
调用 URL 的 GET 请求,我们可以获得轨道的 id
,即 43377447
。
将您的 JS 更改为指向 tracks
端点:
var sound = SC.stream("tracks/43377447", function(sound){
sound.play();
});
对于任何想知道为什么在 Chrome 中流不起作用的人,根据这个 issue,你应该反转实时消息协议以强制 Chrome 使用 html5 而不是闪光灯。
要解决此问题,您必须将 JS 更改为:
SC.stream("tracks/43377447").then(function(sound) {
if (player.options.protocols[0] === 'rtmp') {
player.options.protocols = player.options.protocols.reverse();
}
sound.play();
}
我仍在学习如何使用 API - 这给我带来了麻烦。我相信我的代码是正确的,但是当我尝试使用简单的 JavaScript onclick 函数播放歌曲时没有任何反应。
HTML:
<head>
<script src="https://connect.soundcloud.com/sdk/sdk-3.0.0.js"></script>
<script src="good.js"></script>
</head>
<body>
<button onclick="playIt()">play adele</button>
</body>
JavaScript:
SC.initialize({
client_id: 'XXXXXXXXXX',
});
function playIt(){
var sound = SC.stream("/emberwaves/adele-set-fire-to-the-rain-remix", function(sound){
sound.play();
});
}
首先,使用 resolve
端点获取该轨道的 API URL:
HTTP GET: https://api.soundcloud.com/resolve.json?url=https://soundcloud.com/emberwaves/adele-set-fire-to-the-rain-remix&client_id=[CLIENT_ID]
回复:
HTTP/1.1 302 Found
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Origin
Access-Control-Allow-Methods: GET, PUT, POST, DELETE
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Date
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Date: Thu, 07 Jan 2016 19:54:36 GMT
Location: https://api.soundcloud.com/tracks/43377447.json?client_id=[CLIENT_ID]
Server: am/2
Status: 302 Found
Content-Length: 128
Connection: close
{"status":"302 - Found","location":"https://api.soundcloud.com/tracks/43377447.json?client_id=[CLIENT_ID]"}
调用 URL 的 GET 请求,我们可以获得轨道的 id
,即 43377447
。
将您的 JS 更改为指向 tracks
端点:
var sound = SC.stream("tracks/43377447", function(sound){
sound.play();
});
对于任何想知道为什么在 Chrome 中流不起作用的人,根据这个 issue,你应该反转实时消息协议以强制 Chrome 使用 html5 而不是闪光灯。
要解决此问题,您必须将 JS 更改为:
SC.stream("tracks/43377447").then(function(sound) {
if (player.options.protocols[0] === 'rtmp') {
player.options.protocols = player.options.protocols.reverse();
}
sound.play();
}