如何通过soundcloud.com网址获取Soundcloud嵌入代码

问题描述 投票:16回答:6

我一直在研究几个小时没有运气才能让它奏效。基本上我有一个cms用户可以把soundcloud url这样的“https://soundcloud.com/cade-turner/cade-turner-symphony-of-light”,然后页面可以显示嵌入的iframe。我已经将api文档发送了一段时间,但找不到任何相关内容。这篇文章here已经谈过,但我只是不太明白答案,我检查了oEmbedoEmbed reference,但找不到合适的例子。还有人提示吗?

编辑:感谢Jacob的回答,我终于设法通过使用ajax来实现。

var trackUrl = 'THE_URL';
var Client_ID = 'CLIENT_ID';//you have to register in soundcound developer first in order to get this id 
$.get(//make an ajax request
    'http://api.soundcloud.com/resolve.json?url=' + trackUrl + '&client_id=' + Client_ID, 
    function (result) {//returns json, we only need id in this case
        $(".videowrapper, .exhibitions-image, iframe").replaceWith('<iframe width="100%" height="100%" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/' + result.id +'&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true"></iframe>');//the iframe is copied from soundcloud embed codes
    }
);
javascript php soundcloud
6个回答
11
投票

对于您选择的曲目,嵌入代码如下:

<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/101276036&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true"></iframe>

它唯一独特的是Track ID,在这种情况下它是101276036

因此,当您只有URL时,您真正的问题是尝试找到Track ID,而Soundcloud API提供了一个名为resolve的方法来实现这一点。

require_once 'Services/Soundcloud.php';
$client = new Services_Soundcloud('YOUR_CLIENT_ID');
$track_url = 'https://soundcloud.com/cade-turner/cade-turner-symphony-of-light'; // Track URL
$track = json_decode($client->get('resolve', array('url' => $track_url)));
$track->id; // 101276036 (the Track ID)

然后,您可以存储此ID,或生成并存储要显示的HTML。


24
投票

我在我的代码片段中找到了这个用于您的目的,即使无需注册客户端ID。

//Get the SoundCloud URL
$url="https://soundcloud.com/epitaph-records/this-wild-life-history";
//Get the JSON data of song details with embed code from SoundCloud oEmbed
$getValues=file_get_contents('http://soundcloud.com/oembed?format=js&url='.$url.'&iframe=true');
//Clean the Json to decode
$decodeiFrame=substr($getValues, 1, -2);
//json decode to convert it as an array
$jsonObj = json_decode($decodeiFrame);

//Change the height of the embed player if you want else uncomment below line
// echo $jsonObj->html;
//Print the embed player to the page
echo str_replace('height="400"', 'height="140"', $jsonObj->html);

9
投票

我正在寻找类似的东西,发现这真的很有帮助:

https://developers.soundcloud.com/docs/oembed#introduction

试试这个CURL命令:

curl "http://soundcloud.com/oembed" -d 'format=json' -d 'url=http://soundcloud.com/forss/flickermood'

或者这个jQuery ajax请求:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://soundcloud.com/oembed",
  "method": "POST",
  "headers": {},
  "data": {
    "format": "json",
    "url": "http://soundcloud.com/forss/flickermood"
  }
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

1
投票

这是我写的基于jQuery的Javascript解决方案。

它不需要客户端ID。

确保包含jQuery,并将其添加到文档的<head>中:

<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
    (function ($) {
        $.fn.scembed = function(){
        var datasource  = 'http://soundcloud.com/oembed';
        return this.each(function () {
            var container = $(this);
            var mediasource = $(container).attr("sc_url");
            var params = 'url=' + mediasource + '&format=json&iframe=true&maxwidth=480&maxheight=120&auto_play=false&show_comments=false';
            $.ajaxopts = $.extend($.ajaxopts, {
                                url: datasource,
                                data: params,
                                dataType: 'json',
                                success: function (data, status, raw) {
                                    $(container).html(data.html);
                                },
                                error: function (data, e1, e2) {
                                    $(container).html("Can't retrieve player for " + mediasource);
                                },
                            });
            $.ajax($.ajaxopts);
            });
        };
    })(jQuery);

    $(function(){
        $("div.sc-embed").scembed();
    });
});
</script>

要将Soundcloud播放器插入页面,请创建一个<div>,为其指定一个sc-embed类,并为其指定一个名为sc_url的属性,该属性应设置为Soundcloud页面的URL。

例如:

<div class="sc-embed" sc_url="http://soundcloud.com/forss/flickermood"></div>

要插入多个玩家,请使用<div>的不同值的多个sc_urls。

您可以在Javascript中更改params变量以启用或禁用此处列出的播放器选项:https://developers.soundcloud.com/docs/oembed#introduction


1
投票

我遇到了与上面相同的问题。我有旧的嵌入代码,不再有效,不幸的是,我只限于HTML。看到上面的答案对我来说不起作用。所以我决定,我会尝试一下,看看它是否有效,哦,我的,它确实有效!你不再需要实际转换东西。你可以使用旧网址。这是我的html代码:

<iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay"
src="https://w.soundcloud.com/player/?url=https%3A//soundcloud.com/LPChip/internal-meganism
&color=%23ff5500&auto_play=false&hide_related=false
&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true">
</iframe>

我已添加上面的输入以使其可读,但应该是一行代码。

原因是我在一个自定义BBCode模式的论坛上运行它,我想支持它如下:[soundcloud=Artist]song[/soundcloud]

在上面的例子中,这是[soundcloud=LPChip]Internal-meganism[/soundcloud]

BBCode的实际html如下:

<iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//soundcloud.com/{option1}/{content}&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true"></iframe>

这适用于SMF 2.0.15custom BBCode mod,设置为未解析的相等内容。

但是:Kua zxsw指出


0
投票

我用这种方式在javascript中完成,不使用jQuery:

https://nifflas.lp1.nl/index.php?topic=6630.0
© www.soinside.com 2019 - 2024. All rights reserved.